【问题标题】:Reusing custom UITableViewCells, see old UIView pictures briefly before new pictures load重用自定义 UITableViewCells,在新图片加载前简要查看旧 UIView 图片
【发布时间】:2015-04-13 04:13:45
【问题描述】:

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

在我的 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)
}

【问题讨论】:

    标签: objective-c uitableview uiview uiimageview reuseidentifier


    【解决方案1】:

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

    【讨论】:

    • 我一直在尝试,但没有成功。我已经更新了我的问题以包含我当前的代码
    【解决方案2】:

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

    每当您需要设置新图像时,您只需调用

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

    现在,每当您滚动时,如果未加载图像,您将看到一个占位符图像,而不是看到以前的单元格图像。

    【讨论】:

    • 我已经在使用它来设置我的图像,但它仍然无法正常工作
    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 1970-01-01
    相关资源
    最近更新 更多