【发布时间】:2014-04-01 03:13:48
【问题描述】:
当从 tableView:cellForRowAtIndexPath 调用时,cellForRowAtIndexPath 总是返回 nil:
问题是我实际上是在 tableView:cellForRowAtIndexPath: 内的 block 内调用 cellForRowAtIndexPath:,我猜这可能是问题所在。
我正在使用临时加载(和缓存)的远程图像填充单元格。当图像在完成处理程序块中完全加载时(在 tableView:cellForRowAtIndexPath:) 中,我需要再次获取单元格,因为它可能已经滚动出视图......所以我调用 cellForRowAtIndexPath 再次获取单元格 - cellForRowAtIndexPath 只会返回一个单元格,如果它是可见的。就我而言,我从来没有让它返回任何东西,但为零。即使我滚动得很慢(或很快,或者我尝试过的任何速度......)我得到的都是零。
我会尽快尝试在这里获取一些代码,但在那之前,任何关于为什么会发生这种情况的建议 - 我猜块的事情会是一个提示。
这里是代码:https://dl.dropboxusercontent.com/u/147970342/stackoverflow/appsappsappsappsapps.zip
-tableView:cellForRowAtIndexPath: 实现:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber* numberAppleid = self.appids[indexPath.row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Mycell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Appleid: %@", numberAppleid];
NSString* appleidasstring = [NSString stringWithFormat:@"%@", numberAppleid];
cell.imageView.hidden = YES; // Hide any previous until image loads
[self getAppIconForAppleid:appleidasstring handlerImage:^(UIImage *image) {
if (image == nil) {
return;
}
DLog(@"cellForRowAtIndexPath tableView: %@, indexPath: %@", tableView, indexPath);
UITableViewCell* localcell = [tableView cellForRowAtIndexPath:indexPath];
if (localcell != nil) {
localcell.imageView.image = image;
localcell.imageView.hidden = NO;
}
else {
DLog(@"Nah indexpath is not visible for: %@ because localcell is nil.", appleidasstring);
}
}];
return cell;
}
固定版本:
#define KEYAPPLEID @"keyappleid"
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSNumber* numberAppleid = self.appids[indexPath.row];
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Mycell" forIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"Appleid: %@", numberAppleid];
NSString* appleidasstring = [NSString stringWithFormat:@"%@", numberAppleid];
[cell setAssociativeObject:appleidasstring forKey:KEYAPPLEID];
cell.imageView.hidden = YES; // Hide any previous until image loads
[self getAppIconForAppleid:appleidasstring handlerImage:^(UIImage *image) {
if (image == nil) {
return;
}
NSString* currentappleidofcell = [cell associativeObjectForKey:KEYAPPLEID];
if ([currentappleidofcell isEqualToString:appleidasstring]) {
cell.imageView.image = image;
cell.imageView.hidden = NO;
}
else {
DLog(@"Returning appleid: %@ is not matching current appleid: %@", appleidasstring, currentappleidofcell);
}
}];
return cell;
}
【问题讨论】:
标签: ios objective-c uitableview objective-c-blocks