2017-06-01 52 views
0

我想弄清楚如何在異步模式下使用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; 

}

+0

你的代碼有什麼問題?沒有獲取圖像?或無法在單元格的imageview上傳遞圖像? –

+0

用'__weak ProductCell * cell = [tableView dequeueReusableCellWithIdentifier:@「productCell」forIndexPath:indexPath]; ' –

+0

@Piyush Patel謝謝!有效。 – BSants

回答

1

謝謝@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;