gpt4 book ai didi

ios - 从 tableView :cellForRowAtIndexPath: 内的 block 调用时,cellForRowAtIndexPath 始终为零

转载 作者:行者123 更新时间:2023-11-28 20:03:58 27 4
gpt4 key购买 nike

我有一个问题,当从 tableView:cellForRowAtIndexPath 调用时,cellForRowAtIndexPath 总是返回 nil:

问题是我实际上是在 tableView:cellForRowAtIndexPath: 内的 block 内调用 cellForRowAtIndexPath,我想这可能是问题所在。

我正在用临时加载(和缓存)的远程图像填充单元格。当图像完全加载到完成处理程序 block 中时(在 tableView:cellForRowAtIndexPath: 内)我需要再次获取单元格,因为它可能已经滚出 View ......所以我调用 cellForRowAtIndexPath 再次获取单元格 - cellForRowAtIndexPath 只会返回一个单元格,如果它是可见的。在我的例子中,除了 nil 之外,我从来没有让它返回任何东西。即使我滚动得非常慢(或很快,或者我尝试过的任何速度......)我得到的都是零。

我会尝试尽快在此处获取一些代码,但在此之前,任何关于为什么会发生这种情况的建议 - 我想 block 的东西会是一个提示。

这是代码: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;
}

并且需要这个类别:https://stackoverflow.com/a/10319083/129202

最佳答案

如果 block 在 tableView:cellForRowAtIndexPath: 回调中,您可以直接在 block 中引用单元格。该 block 将自动保留单元格,直到 block 消失。但是,请注意保留循环。如果该 block 由单元格拥有,则必须使用对该单元格的弱引用。

所以你的实现看起来像这样:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber* numberAppleid = self.appids[indexPath.row];

// Create your own UITableViewCell subclass that has an "appleidasstring" property
MyTableViewCell* 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

// Set the appleidasstring on the cell to be checked later
cell. getAppIconForAppleid:appleidasstring = appleidasstring;

// Modify the handler callback to also callback with the appleidasstring
[self
getAppIconForAppleid:appleidasstring
handlerImage:^(UIImage *image, NSString *imageAppleIdaString)
{
if (image == nil) {
return;
}

// Ensure the cell hasn't been repurposed for a
// different imageAppleIdaString
if ([cell.appleidasstring isEqualToString:imageAppleIdaString]) {
cell.imageView.image = image;
cell.imageView.hidden = NO;
}
}
];

return cell;
}

关于ios - 从 tableView :cellForRowAtIndexPath: 内的 block 调用时,cellForRowAtIndexPath 始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22776134/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com