类似微博自定义cell添加图片,通常会导致图片重用
- 解决方法:手动懒加载图片控件,然后在加载图片之前先清除图片控件(核心就是在cell重用的时候不让图片重用).
代码:
// 1. 图片控件使用懒加载
- (UIImageView *)imageView1
{
if (!_imageView1) {
_imageView1 = [[UIImageView alloc] init];
_imageView1.backgroundColor = [UIColor greenColor];
}
return _imageView1;
}
// 2.在重用设置数据时,先移除图片控件
- (void)setTopic:(YTopic *)topic
{
// 重用时,把显示图片的控件移除
[self.imageView1 removeFromSuperview];
// 3.在添加的图片控件的时候懒加载图片
self.imageView1.frame = CGRectMake(InsetMargin, imgViewY, imgViewW, imgViewH);
[self.imageView1 sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",SERVERURL,imageURLArray[0]]] placeholderImage:nil];
[self addSubview:self.imageView1];
注意:核心就是不让图片控件重用