重用自定义UITableViewCells,在加载新图片之前,请简要查看旧的UIView图片



我有一个自定义的UITableViewCell,它包含一个UIView,我在其中显示了几张图片。

在我的tableView:cellForRowAtIndexPath:函数中,我调用dequeueReusableCellWithIdentifier:来重用旧的自定义单元格,然后用新图片更新它们。

问题是,当我在屏幕上快速滚动时,在加载新图片之前,我会看到旧图片的闪烁,这很不吸引人。

我试图通过以下方式解决此问题:

  • 在自定义TableViewCell实现文件中实现prepareForReuse;这导致同样的三个UIView一遍又一遍地出现,在这种情况下,新图片完全停止加载
  • 在调用dequeueReusableCellWithIdentifier后立即清除UIView,方法是使用for循环删除所有子视图;这个应用程序现在加载图片需要很长时间

修复此问题的最佳方法是什么?为什么在我尝试的修复中会出现上述错误?这是我当前的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Blah";
blahCell *someCell = (blahCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!someCell) {
//initialize cell
} else {
someCell.imageContainerView = nil;
}
... (other code here)
}

作为表视图数据源方法cellForRowAtIndexPath的第一步,我会尝试将单元格的imageView属性设置为nil

您可以通过AFNetworking使用此UIImageView类别

每当你必须设置一个新的图像,你只需要调用

[someCell.imageContainerView setImageWithURL:url placeholderImage:placeholderImage];

现在,无论何时滚动,如果未加载图像,您都会看到占位符图像,而不是以前的单元格图像。

最新更新