昨天在項目中發(fā)現(xiàn)一個問題,一個列表滾動到底部時,刷新列表,會向上彈一下,百思不得其解,經(jīng)過一番查詢,原因是刷新列表時,tableView會重新計算高度,得到用緩存行高的辦法如下:
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
NSNumber *heigt = self.cellheightDic[key];
if (heigt == nil) {
heigt = @(100);
}
return heigt.floatValue;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"heig = %ld",indexPath.row);
NSString *key = [NSString stringWithFormat:@"%ld",indexPath.row];
NSNumber *heigt = self.cellheightDic[key];
if (heigt == nil) {
heigt = @(self.cell.cellHeight);
[self.cellheightDic setValue:heigt forKey:key];
}
return heigt.floatValue;
}