【问题标题】:UITableView asynchronous UIImage settingUITableView 异步 UIImage 设置
【发布时间】:2014-02-25 18:12:19
【问题描述】:
我的目标是设置文本,然后异步调用图像,并在到达时设置图像。
我目前在UITableViewCell 中设置图像的异步调用,并注意到我之前的技术导致了我最近发现的循环:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:@"id"];
如果(细胞 == 零){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
}
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:userAvatar]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
如果(数据){
UIImage* avatarImage = [UIImage imageWithData:data];
如果(头像图像){
dispatch_async(dispatch_get_main_queue(), ^{
[cell.imageView setImage:avatarImage];
});
/*
.--. [停止]
.----''--。 |
'-()-----()-' |
如果我包括以下内容,
程序进入一个循环,
并且尚未确定解决方法:
dispatch_async(dispatch_get_main_queue(), ^{
[_tableView 重载数据];
});
[走] 。 - 。
| .----''--。
| '-()-----()-'
*/
}
}
}];
cell.textLabel.text = @"foo";
cell.detailTextLabel.text = @"bar";
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
返回单元格;
}
是否有一种方法可以在没有自定义类中的自定义设置器的情况下产生我正在寻找的结果?
【问题讨论】:
-
请勿从cellForRow... 致电reloadData。坏事发生。查看 Apple 的 LazyTableImages 示例应用程序,了解如何正确执行此操作。
-
您也可以使用 AFNetworking 轻松做到这一点 - 检查 UIImageView+AFNetworking.h 的添加。
-
@joao AFNetworking 使用了一种非常简单的方法,基本上“让它工作”,但实际上引入了一个更严重的问题。所以,我不会推荐它。
-
@CouchDeveloper 你能告诉我有什么问题吗?我使用 AFNetworking 已经有一段时间了,从来没有出现过任何奇怪的行为。
-
@joao 简单方法的根本问题是,请求(用于加载图像)可能会快速启动和取消 -取决于用户滚动表格视图的速度。在最坏的情况下,这可以每秒启动数百个请求,这些请求将由 Web 服务完全服务,但随后会被客户端立即取消,客户端永远不会接收数据,也永远不会在本地缓存数据。跨度>
标签:
ios
objective-c
asynchronous
uitableview