0

您好在这里找到了答案: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;
}

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

4

2 回答 2

1

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

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

于 2013-01-21T15:07:39.440 回答
0

图片是从网上下载的吗?

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

看看这个例子……

关于 Apple 的 LazyTableImages 示例的问题 - 与应用商店的行为不完全相同

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

于 2013-01-21T14:45:15.313 回答