iOS-TableView性能优化方案

作者: StevenHu_Sir | 来源:发表于2019-08-14 22:29 被阅读0次

卡顿原因:cell赋值内容时,会根据内容设置布局,也就可以知道cell的高度,若有1000行,就会调用1000次 cellForRow方法,而我们对cell的处理操作,都是在这个方法中赋值,布局等等,开销很大。

1.提前计算并缓存好高度(布局),因为heightForRowAtIndexPath:是调用最频繁的方法

在获得数据后,直接先根据数据源计算出对应的布局,并缓存到数据源中,这样在tableView:heightForRowAtIndexPath:方法中就直接返回高度,而不需要每次都计算了

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dict = self.dataList[indexPath.row];
CGRect rect = [dict[@"frame"] CGRectValue];
    return rect.frame.height;
}

2.==赋值和计算布局分离==。cellForRow负责赋值,heightRorRow负责计算高度。

3.自定义cell绘制异步绘制

给自定义的Cell添加draw方法
采用异步绘制,如果在重写drawRect方法就不需要用GCD异步线程了,因为drawRect本来就是异步绘制的。

4.滑动UITableView时,按需加载对应的内容(大量图片展示,网络加载的时候很管用)

//按需加载 - 如果目标行与当前行相差超过指定行数,只在目标滚动范围的前后指定3行加载。
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    NSIndexPath *ip = [self indexPathForRowAtPoint:CGPointMake(0, targetContentOffset->y)];
    NSIndexPath *cip = [[self indexPathsForVisibleRows] firstObject];
    NSInteger skipCount = 8;
    if (labs(cip.row-ip.row)>skipCount) {
        NSArray *temp = [self indexPathsForRowsInRect:CGRectMake(0, targetContentOffset->y, self.width, self.height)];
        NSMutableArray *arr = [NSMutableArray arrayWithArray:temp];
        if (velocity.y<0) {
            NSIndexPath *indexPath = [temp lastObject];
            if (indexPath.row+33) {
                [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-3 inSection:0]];
                [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-2 inSection:0]];
                [arr addObject:[NSIndexPath indexPathForRow:indexPath.row-1 inSection:0]];
            }
        }
        [needLoadArr addObjectsFromArray:arr];
    }
}
//在tableView:cellForRowAtIndexPath:方法中加入判断:
if (needLoadArr.count>0&&[needLoadArr indexOfObject:indexPath]==NSNotFound) {
    [cell clear];
    return;
}

滚动很快时,只加载目标范围内的Cell,这样按需加载,极大的提高流畅度

5.减少subviews的数量

6.在heightForRowAtIndexPath:中尽量不使用cellForRowAtIndexPath:,如果你需要用到它,只用一次然后缓存结果。

7.尽量少用addView给Cell动态添加View,可以初始化时就添加,然后通过hide来控制是否显示

相关文章

  • iOS-TableView性能优化方案

    卡顿原因:cell赋值内容时,会根据内容设置布局,也就可以知道cell的高度,若有1000行,就会调用1000次 ...

  • Spark 性能优化方案

    Spark 性能优化方案(转自李智慧的Spark性能优化方案): Spark性能测试工具 •Spark性能测试基准...

  • mysql程序

    MySQL性能优化方案总结

  • iOS-tableView的性能优化

    1.tableViewCell复用机制 简单介绍:假设我们的tableview中有1000个cell,窗口只容得下...

  • iOS 性能优化

    参考文章 微信读书 iOS 性能优化总结 iOS性能优化 iOS 启动连续闪退保护方案 iOS代码性能优化《阶级篇...

  • iOS 性能优化二

    主要讲解界面卡顿原因/优化方案/离屏渲染 iOS 性能优化一iOS 性能优化二iOS 性能优化三 1. 开发中遇到...

  • 收集_Android源码文章

    一、Bitmap: Android bitmap压缩优化方案Android性能优化系列之Bitmap图片优化 二、...

  • 学习过的好文章(不定期更新ing)

    性能优化 bestswifter结合Instrument分析影响性能的因素,提出优化方案并解释背后的原理UIKit...

  • iOS 优化方案

    一、性能优化基本方案 1、卡顿原因以及避免方案2、耗电优化3、启动优化4、安装包瘦身 二、卡顿优化原因以及避免方案...

  • 性能优化方案

    APM Application performance management,即应用性能管理,通过对应用的可靠性、...

网友评论

    本文标题:iOS-TableView性能优化方案

    本文链接:https://www.haomeiwen.com/subject/vtsojctx.html