gpt4 book ai didi

ios - 滚动时更新 UITableViewCell

转载 作者:行者123 更新时间:2023-11-29 12:30:44 25 4
gpt4 key购买 nike

我正在尝试将图像从服务器异步加载到单元格。但是图像在滚动时不会改变,只有在滚动停止后才会改变。 “已加载”消息仅在滚动停止后才会出现在控制台中。我希望图像在滚动时显示在单元格中。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = (CustomCell *)[_tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
ZTVRequest *request = [[ZTVRequest alloc] init];
[request getSmallImg completionHandler:^(UIImage *img, NSError *error) {
if (! error) {
NSLog(@"loaded")
cell.coverImgView.image = img;
}
}];

return cell;
}

最佳答案

我正在使用 NSURLConnection 加载图像。我在这个答案中找到了解决方案:https://stackoverflow.com/a/1995318/1561346作者:丹尼尔·迪克森

这里是:

在您停止滚动之前连接委托(delegate)消息不会触发的原因是因为在滚动期间,运行循环处于 UITrackingRunLoopMode 中。默认情况下,NSURLConnection 仅在 NSDefaultRunLoopMode 中自行安排,因此您在滚动时不会收到任何消息。

以下是如何在“常见”模式下安排连接,其中包括 UITrackingRunLoopMode:

NSURLRequest *request = ...
NSURLConnection *connection = [[NSURLConnection alloc]
initWithRequest:request
delegate:self
startImmediately:NO];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSRunLoopCommonModes];
[connection start];

请注意,您必须在初始化程序中指定 startImmediately:NO,这似乎与 Apple 的文档背道而驰,该文档建议您即使在启动后也可以更改运行循环模式。

关于ios - 滚动时更新 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27806500/

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