美文网首页
iOS TableView 优化二

iOS TableView 优化二

作者: 赵哥窟 | 来源:发表于2021-05-25 11:33 被阅读0次

之前写过一篇关于朋友圈的TableView优化。
iOS TableView 优化

今天仿照微博写了一个demo,滚动FPS都是60,没发现卡顿的情况。


Simulator Screen Shot - iPhone 12 Pro Max - 2021-05-25 at 11.19.58.png

头部和底部我是用Masonry布局,为了减少计算量,这个部分可以用frame,

- (void)setupView{
    self.avatarImageView = [UIImageView new];
    [self addSubview:self.avatarImageView];
    [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptW(5));
        make.left.mas_equalTo(AdaptW(16));
        make.width.height.mas_equalTo(AdaptW(40));
    }];
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = AdaptFont(15);
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(AdaptW(5));
        make.left.mas_equalTo(self.avatarImageView.mas_right).offset(AdaptW(10));
        make.right.mas_equalTo(AdaptW(-16));
    }];
    
    self.infoLabel = [UILabel new];
    [self addSubview:self.infoLabel];
    [self.infoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom).mas_offset(AdaptW(1));
        make.left.mas_equalTo(self.avatarImageView.mas_right).offset(AdaptW(10));
        make.right.mas_equalTo(AdaptW(-16));
    }];
}

底部

- (void)setupView{
    
    NSArray *imgAry = @[@"retweet",@"comment",@"like"];
    
    NSMutableArray *tolAry = [NSMutableArray new];
    for (int i = 0; i < imgAry.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:imgAry[i]] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        [btn setTag:i+1000];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
        [self addSubview:btn];
        [tolAry addObject:btn];
    }
    //水平方向控件间隔固定等间隔
    [tolAry mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:0 leadSpacing:0 tailSpacing:0];
    
    self.sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 38, kScreenWidth, 8)];
    self.sectionView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.2];
    [self addSubview:self.sectionView];
}

朋友圈和微博写在一个项目里面的,这只是提供一种优化的思路,对于初步了解TableView优化的朋友可以参考。

Demo

总结下来注意以下几点基本可以解决大部分卡顿。
1.不要阻塞主线程,绘制或者计算量大可以放到子线程执行。
2.提前计算并缓存高度。
3.减少xib或者Storyboard的使用,最好用frame布局方式,减少动态布局的计算。
4.有的时候后台会返回原图,这样加载图片很慢,可以让返回压缩后的图片。

其实能做到这几点已经能够解决大部分卡顿了,其他的可以用结合FPS监测工具或者Xcode自带的调试工具Instruments来看看界面的流畅度。

相关文章

网友评论

      本文标题:iOS TableView 优化二

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