搜索
您的当前位置:首页正文

iOS tableView局部刷新界面闪的问题

来源:二三娱乐

1、不使用iOS自动布局Autolayout。
解决办法:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0; 

2、使用自动布局,cell会自适应高度。
在iOS10及以下,刷新cell,cell会跳动。
解决办法:

// 定义一个可变数组记录cell的历史高度
@property (nonatomic, strong) NSMutableDictionary *cellHightDict;// 记录cell高度的数组

// 之后,实现方法
- (NSMutableDictionary *)cellHightDict {
    if (!_cellHightDict) {
        _cellHightDict = [NSMutableDictionary new];
    }
    return _cellHightDict;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cellHightDict setObject:@(cell.frame.size.height) forKey:[NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section, (long)indexPath.row]];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = [[self.cellHightDict objectForKey:[NSString stringWithFormat:@"%ld_%ld",(long)indexPath.section, (long)indexPath.row]] floatValue];
    if (height == 0) {
        return 100;
    }
    return height;
}
Top