[英]UITableviewCell returning as nil inside block
我对tableView:cellForRowAtIndexPath:
实现如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BSProductCell *cell = [tableView dequeueReusableCellWithIdentifier:BSProductCellIdentifier];
cell.productImageView.image = [UIImage imageNamed:@"Placeholder"];
[[BSImageLoader sharedInstance] getImageWithURL:self.dataSourceArray[indexPath.row] withCompletionBlock:^(UIImage *image, BOOL fromCache, NSString *error){
BSProductCell *tableCell = (BSProductCell *)[self.tableView cellForRowAtIndexPath:indexPath];
[tableCell.productImageView setImage:image];
NSLog(@"%@", tableCell);
}];
return cell;
}
getImageWithURL:withCompletionBlock:
如果在本地缓存中找不到图像,则从指定的URL下载图像,或者仅从缓存中返回图像。 首先,在加载视图并填充单元格时,所有事情都会按预期进行:先下载图像,然后将其放置在单元格中。 问题是当我开始滚动表视图时,NSLog显示(null)
。 我只是不知道为什么tableCell
对象为null。
PS:我在遵循Apple几乎相同的示例之后编写了示例。 我也检查了,并且self.tableView
和indexPath
是零。
该块是异步的,您应该在该块外部创建单元格,并且只在其中设置图像:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BSProductCell *cell = [tableView dequeueReusableCellWithIdentifier:BSProductCellIdentifier];
if(cell==nil){
cell = [[BSProductCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:BSProductCellIdentifier];
}
cell.productImageView.image = [UIImage imageNamed:@"Placeholder"];
[[BSImageLoader sharedInstance] getImageWithURL:self.dataSourceArray[indexPath.row] withCompletionBlock:^(UIImage *image, BOOL fromCache, NSString *error){
//Here you check that the cell is still displayed
if([tableView cellForRowAtIndexPath:indexPath]){
[cell.productImageView setImage:image];
NSLog(@"%@", cell);
}
}];
return cell;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.