【问题标题】:Images getting mixed up when scrolling in a UITableView在 UITableView 中滚动时图像混淆
【发布时间】:2013-01-04 15:28:14
【问题描述】:

您好在这里找到了答案:Images getting mixed up in a UITableView - XML parsing

我正在解析一个 XML 文件,其中包含指向我要放入 UITable 的图像的链接。由于某种原因,当我在表格中向下滚动时,这些图片完全混淆了。这是我用于 UITable 的代码:

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

    static NSString *CellIdentifier = @"Cell";
    Tweet *currentTweet = [[xmlParser tweets] objectAtIndex:indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        CGRect imageFrame = CGRectMake(2, 8, 40, 40);
        customImage = [[UIImageView alloc] initWithFrame:imageFrame];
        [cell.contentView addSubview:customImage];

    }

    NSString *picURL = [currentTweet pic];
    if (![picURL hasPrefix:@"http:"]) {
        picURL = [@"http:" stringByAppendingString:picURL];
    }

    customImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:picURL]]];

    return cell;
}

知道我做错了什么吗?非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: ios objective-c xml uitableview xml-parsing


    【解决方案1】:

    这是因为您将可重复使用的单元格出列。它正在回收一个包含以前使用过的图像的单元格。

    我注意到您将图像设置在与初始化单元格相同的 if 块中。如果我是你,我会把[cell.contentView addSubview:customImage]; 移到你的退货声明之前。这样,当它回收一个出队的单元格时,它不会有图像。

    【讨论】:

      【解决方案2】:

      图片是从网上下载的吗?

      如果是这样,那么下载图像的延迟将导致此问题。您需要以多线程方式执行此操作。

      看看这个例子...

      Question about Apple's LazyTableImages sample - Doesn't behave exactly like the app store

      网上也有很多关于延迟加载图片和UITableViews的教程。

      【讨论】:

        猜你喜欢
        • 2018-06-14
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多