2015-03-31 67 views
1

这里的主要问题是我正在将图像加载到自定义单元格中,并且在滚动时,重新使用的单元格会在加载新图像时显示旧图像。重新使用的单元格在刷新图像时滞后

坦率地说,不会有很多细胞,所以我的第一个想法是,我可以避开使用tableView.dequeueReusableCellWithIdentifier,但我期待着尝试的每个地方,并找出这个问题或者人们质疑为什么有人会这样做,或者他们只是提供模糊的建议而没有某种例子。

什么是最好的方式来显示没有这个滞后的单元格重新处理图像?

enter image description here

class FavoritesPropertyViewCell: UITableViewCell { 
    @IBOutlet weak var activityIndicator: UIActivityIndicatorView! 
    @IBOutlet weak var cellPropertyImage: UIImageView! 
    @IBOutlet weak var cellRoomsValue: UILabel! 
    @IBOutlet weak var cellSpaceValue: UILabel! 
    @IBOutlet weak var cellPriceRangeValue: UILabel! 
    @IBOutlet weak var cellCityStateZipValue: UILabel! 
    @IBOutlet weak var cellAddressValue: UILabel! 

} 

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{   


      var newPropertyCell: FavoritesPropertyViewCell = tableView.dequeueReusableCellWithIdentifier("FavoritesPropertyCell", forIndexPath: indexPath) as FavoritesPropertyViewCell 

//... Get image, format values ... 


       // Put everything in the labels. 
       newPropertyCell.cellRoomsValue.text = roomsValue 
       newPropertyCell.cellSpaceValue.text = spaceValue 
       newPropertyCell.cellPriceRangeValue.text = priceValue 
       newPropertyCell.cellAddressValue.text = addressValue 
       newPropertyCell.cellCityStateZipValue.text = cityStateZipValue 

       return newPropertyCell 

    } 
+0

如何在不使用tableView.dequeueReusableCellWithIdentifier的情况下使用它? – 2015-03-31 21:02:21

回答

1

当您使用滚动因为速度VS网络加载速度的标准的UIImageView它解决不了的问题。

我的经验表明,它肯定需要使用图像缓存。 有一些的CocoaPods的实现图像缓存,你可以检查AsyncImageView或NPRImageView

+0

我最终加入了一个图像缓存,并取得了巨大的差异。再次感谢。 – 2017-05-09 17:05:27

0

如果你正在加载从网络上的形象,尝试设置新的图像前设置cell.image = nil。它将至少清除图像,而下一个加载到它的位置。

+0

由于这是一个自定义单元格,图像必须以不同的方式进行引用,我试图将其设置为零,但它给了我一个合适的位置。我今天尝试了几种不同的方式,但明天我会再次尝试,并看看是否有我错过的东西。谢谢。 – 2015-04-01 01:49:21

1

您应该使用一个占位符图像,当细胞被初始化和重用的图像设置为,把这样的事情在你的MyCell.m:

- (void)awakeFromNib { 

    [super awakeFromNib]; 

    UIImage *placeHolder = [UIImage imageNamed:@"profilePlaceholder"]; 
    self.propertyImage=placeholder; 

} 

-(void)prepareForReuse { 

    //this is called each time we reuse a cell 
    UIImage *placeHolder = [UIImage imageNamed:@"profilePlaceHolder"]; 
    self,propertyImage=placeHolder; 

} 

现在什么也不想做的就是创建图像缓存这可能只是一个的NSMutableDictionary,然后在你的创作做这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 
    UIImage *cachedImage = self.images[cell.propertyName]; 
      if (cachedImage) { 
       //use cached image 
       cell.propertyImage=cachedImage; 
      } 

      else { 

       //your image download code here 



       //then add the image to the dictionary 
       [self.images setObject:downloadedImage forKey:cell.popertyName]; 

       //and set it 
       cell.propertyImage = downloadedImage; 
} 

这些都是基础知识反正。

,如果你不希望使用离队然后就改变细胞创建于:

MyCell *cell = [[MyCell init] alloc]; 

或无论你初始化你的...

+0

如何在不使用tableView.dequeueReusableCellWithIdentifier的情况下做到这一点? – 2015-03-31 21:02:44

+0

你为什么不想使用出列队列?它只会让你的桌子更加高效,而且很容易设置。 – Kex 2015-03-31 21:22:22

+0

编辑我的答案... – Kex 2015-03-31 21:25:07

0

我不能找到一种方法,步骤在使用tableView.dequeueReusableCellWithIdentifier之前,并没有把单元格放到它自己的xib中,然后以这种方式调用(我可能最终会从长远来看),但为了防止出现这种重新加载的图像,现在我只是简单地在我实例化newPropertyCell后,将图像设置为占位符。

引用一个不存在的图像将不会带回任何我想要发生的事情。

注意:“占位符”是对不存在但不是错误的文件的引用,它不返回任何内容。

var newPropertyCell: FavoritesPropertyViewCell = tableView.dequeueReusableCellWithIdentifier("FavoritesPropertyCell") as FavoritesPropertyViewCell 

newPropertyCell.cellPropertyImage.image = UIImage(named: "placeholder") 

在以后的时间我会回来并缓存图像,以便有更大的持久性,我不会有打API来检查图像,或因为它是滚动重新下载。

谢谢大家!

+0

注意事项:将图像设置为“无”不能迅速工作。 – 2015-04-01 16:53:53