UITableView出现卡顿如何处理

tableView的beginUpdate和endUpdate要比reloadData和reloadRowsAtIndexPaths好,因为beginUpdate和endUpdate会执行一个动画block,图片加载的时候显的很平滑。你自己试一下就知道了。

加载图片的时候要用多线程,要用缓存,也就是需要异步加载

计算cell的高度的时候要尽量的简单,因为tableVIew中cell的高度是一次性加载完的

要用重用机制,一定要用,不然会卡的

用户习惯性快速的滚动,视图和数据内容都会快速的变化,如果效率问题处理不好,很容易有卡顿的现象。造成用户体验的降低。

数据刷新

如果我们的modle更新了。相应的要体现到UITableView上面。简单的我们可以reload整个TableView。这样做很方便,而且数据上没有问题。唯一的问题就是,reload整个TableView的效率太低了。而且,往往我们只是少数的Cell内容变化。所以没有必要去reload整个TableView。而是那条数据变化去刷新对应的Cell就好了。这样做效率提高很多。

具体涉及到的几个函数

- (void)beginUpdates;
- (void)endUpdates;
 
- (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;
 
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation: (UITableViewRowAnimation)animation;
- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

  

我自己遇到的情况:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    
    switch (indexPath.row) {
        case 0:
            // 初始化抢红包Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_rob forIndexPath:indexPath];
            [self setupRobRedPackageCell:cell];
            break;
        case 1:
            // 初始化轮播Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_ad forIndexPath:indexPath];
            [self setupAdCarouseCell:cell];
            break;
        case 2:
            // 初始化趣味答题Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_answer forIndexPath:indexPath];
            [self setupInterestingAnswerCell:cell];
            break;
        case 3:
            // 初始化排行榜Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_charts forIndexPath:indexPath];
            [self setupChartsCell:cell];
            break;
        case 4:
            // 初始化折扣优惠区Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_discount forIndexPath:indexPath];
            [self setupDiscountCell:cell];
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

其中的[self setupxxxx]用来创建和添加各种视图到Cell的contentView中, 由于没有判断仔细, 导致每次重用cell的时候都会调用到, 所以照成了卡顿, 以下是修改后的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    
    switch (indexPath.row) {
        case 0:
            // 初始化抢红包Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_rob forIndexPath:indexPath];
            if (self.cellReuseTag >= 1) break;
            [self setupRobRedPackageCell:cell];
            self.cellReuseTag = 1;
            break;
        case 1:
            // 初始化轮播Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_ad forIndexPath:indexPath];
            if (self.cellReuseTag >= 2) break;
            [self setupAdCarouseCell:cell];
            self.cellReuseTag = 2;
            break;
        case 2:
            // 初始化趣味答题Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_answer forIndexPath:indexPath];
            if (self.cellReuseTag >= 3) break;
            [self setupInterestingAnswerCell:cell];
            self.cellReuseTag = 3;
            break;
        case 3:
            // 初始化排行榜Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_charts forIndexPath:indexPath];
            if (self.cellReuseTag >= 4) break;
            [self setupChartsCell:cell];
            self.cellReuseTag = 4;
            break;
        case 4:
            // 初始化折扣优惠区Cell
            cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier_discount forIndexPath:indexPath];
            if (self.cellReuseTag >= 5) break;
            [self setupDiscountCell:cell];
            self.cellReuseTag = 5;
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

简单的, 也就是搞多一个标记来记录cell已经初始化过, 跳过初始化的步骤

原文地址:https://www.cnblogs.com/Rinpe/p/5065051.html