gpt4 book ai didi

ios - UITableView 在滚动中改变图像

转载 作者:行者123 更新时间:2023-11-29 03:37:32 27 4
gpt4 key购买 nike

每次滚动 TableView 时,我的图像都会变得困惑,主要是第一行。我真的不知道该怎么办。

这是代码:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";

BarCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

[cell.activityFotoBar startAnimating];
cell.activityFotoBar.hidesWhenStopped = YES;

if(!cell){
cell = [[BarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}

NSMutableDictionary *infoBar = [self.bares objectAtIndex:indexPath.row];
NSString *nomeImagem = [infoBar objectForKey:@"foto"];

NSURL *url = [NSURL URLWithString:nomeImagem];
NSURLRequest *requestImagem = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:requestImagem queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if(connectionError == nil){
cell.imageViewImagemBar.image = [UIImage imageWithData:data];
[cell.activityFotoBar stopAnimating];
}

}];


cell.labelNomeBar.text = [infoBar objectForKey:@"nome"];
cell.labelEnderecoBar.text = [infoBar objectForKey:@"endereco"];
cell.labelAvaliacaoBar.text = [NSString stringWithFormat:@"Votos: %@", [infoBar objectForKey:@"votos"]];

return cell;

}

提前致谢!

最佳答案

问题的发生是因为异步图像请求在您的单元格滚出屏幕并被重用后完成。下载完成“无序”,导致视觉困惑。本质上,一些通过滚动重新使用的单元格仍然是“热的”,因为它们的图像正在进行加载。重复使用此类单元会在新旧图像下载之间造成竞争。

您应该更改用于加载图像的策略:不要发送请求并“忘记”它,而是考虑使用 connectionWithRequest:delegate: 方法,将连接存储在单元格中,并且当调用 prepareForReuse 方法时调用 cancel。这样你重复使用的细胞就会“冷”。

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

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