使用AFNetworking 3.0(异步)从URL设置TableView细胞图像
问题描述:
我想弄清楚如何在异步模式下使用URL和AFNetworking 3.0在我的TableViewCell中设置imageView。使用AFNetworking 3.0(异步)从URL设置TableView细胞图像
我不知道如何正确使用它。正是在这一行:
cell.productImageView.image = image;
它没有工作。我不能直接归因于它,对吧?
这里是我的代码:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ProductCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productCell" forIndexPath:indexPath];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"MY_URL"]];
[cell.imageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
cell.productImageView.image = image;
} failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
return cell;
}
答
谢谢@Piyush帕特尔!它的工作,我可以加载图像在cellViewView。在这里,我做了什么:
__weak ProductCell *weakCell = cell;
[cell.imageView setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
weakCell.productImageView.image = image;
} failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
return cell;
你的代码有什么问题?没有获取图像?或无法在单元格的imageview上传递图像? –
用'__weak ProductCell * cell = [tableView dequeueReusableCellWithIdentifier:@“productCell”forIndexPath:indexPath]; ' –
@Piyush Patel谢谢!有效。 – BSants