cell 的优化问题(自己项目中遇到的)

要求是:在cell最下面添加scrollView,然后在scrollView上面添加图片,这里就需要对cell复用问题进行优化。不对之处还望指出,谢谢!

假如我第一个cell上面需要显示3张图片,复用第一个cell的时候,假如这个时候需要显示5张图片,我们此时只需要再创建2张图片的button就可以了。如果这个时候需要显示少于3张图片的话,我们此时只需要删除掉后两个button就可以了。然后只需要重新绘制button上面的图片就可以了。

for (UIView *subView in _scrollView.subviews) {
        if (subView.tag >= StartTag + _message.pictures.count) {
            [subView removeFromSuperview];
        }
    }

if (_message.pictures == nil || _message.pictures.count <= 0) {
        
    } else {
        //图片处理
        for (int i = 0; i<_message.pictures.count; i++) {
            int tag = StartTag + i;
            UIButton *imageBtn = [_scrollView viewWithTag:tag];
            if (imageBtn == nil) {
                imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(i * (_scrollView.height + 10), 0, _scrollView.height, _scrollView.height)];
                imageBtn.tag = tag;
                [_scrollView addSubview:imageBtn];
            }
            
            NSURL *url = [NSURL URLWithString:_message.pictures[i]];
            [imageBtn sd_setImageWithURL:url forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"nouse"] options:SDWebImageAllowInvalidSSLCertificates completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                
            }];
        }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容