2014-10-18 43 views
1

我有一个UITableViewCell显示文本和可选的图像。然而,当图像显示时,它变得有点bug,因为在-(void)prepareForReuse:我将tableViewCellimageCell设置为零,并且滚动图像需要每次加载。UITableView滚动越野车由于prepareForReuse当图像在单元格中

在我customTableViewCell.m,这是我使用以备再用代码:

- (void)prepareForReuse { 
    [super prepareForReuse]; 

    self.noteLabel.text = nil; 
    self.textLabel.text = nil; 
    self.imageCell.image = nil; 
    self.personImage = nil; 
} 

通过删除行self.imageCell.image = nil;一些细胞会复制其它UITableViewCellsimageCell,所以我必须使用prepareForReuse方法。

有没有什么办法可以将imageCell设置为零,如果它有所有的单元格加载时的图像?我试过

if(self.iamgeCell.image == nil){ 
    self.imageCell.image = nil; 
} 

在我试着说:如果imageCell重用之前为空,请将其设置为nil重用准备的时候,却没有工作这么好。

该如何我目前加载图像中的cellForRowAtIndex方法:

PFFile *imageFile = [payment objectForKey:@"img"]; 
    if(![imageFile isEqual:@""]){ 
     [imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) { 
     if (!error) { 
      UIImage *image = [UIImage imageWithData:imageData]; 
      cell.imageCell.image = [self imageWithImage:image scaledToWidth:cell.imageCell.frame.size.width ]; 
      } 
     }]; 
     } 
+0

这是一个“它不是越野车 - 这就是它的工作方式!” – Fattie 2014-10-18 14:25:54

回答

2

几件事情。在此代码发布的逻辑是倒退,而不会做任何事情:

if(self.iamgeCell.image == nil){ 
    self.imageCell.image = nil; 

在英语中,说:“如果图像是零,将其设置为nil你想反向:

if(self.iamgeCell.image != nil){ 
    self.imageCell.image = nil; 

英文表示“如果图像不为零,则将其设置为零。

但是你总是希望它为零,所以为什么检查,只需将它设置为无prepareForReuse。

self.imageCell.image = nil; 

我通常不会实现prepareForReuse。相反,在我的数据源方法中,我总是完全配置一个单元格,将所有字段设置为显式值(图像,如果不需要图像,则为零)将此视为重新使用纸质表单。您必须清除以前用户写入的所有字段。

+0

是的,我知道,所以这就是我现在的prepareForReuse中的东西,唯一的一点是它每次显示图像时都会加载图像,导致滚动中出现轻微故障 – bdv 2014-10-18 14:41:55

+0

您是在讨论从磁盘加载图像还是下载它?您需要使用网络的异步下载。在任何一种情况下,如果每个单元格的图像总是相同,请将该图像放在适当位置。只擦除单元格之间可以更改的数据。或者,如果它是有时显示的静态图像而不是其他图像,则隐藏图像(如果未使用),并在显示图像时取消隐藏。 – 2014-10-19 11:42:03