gpt4 book ai didi

ios - 在 UITableView 中滚动时图像变得困惑

转载 作者:行者123 更新时间:2023-11-29 04:10:44 37 4
gpt4 key购买 nike

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

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

最佳答案

这是因为您正在使可重用单元出队。它正在回收其中包含以前使用过的图像的单元格。

我注意到您正在将图像设置在与初始化单元格相同的 if block 中。如果我是你,我会将 [cell.contentView addSubview:customImage]; 移到 return 语句之前。这样,当它回收出列的单元格时,其中就不会包含图像。

关于ios - 在 UITableView 中滚动时图像变得困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14440958/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com