从tableView中的块调用时,cellForRowAtIndexPath总是为nil:cellForRowAtIndexPath:

时间:2014-04-01 03:13:48

标签: ios objective-c uitableview objective-c-blocks

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

问题是我实际上在tableView:cellForRowAtIndexPath中的中调用了cellForRowAtIndexPath:我猜这可能是问题所在。

我正在使用加载(和缓存)ad hoc的远程图像填充单元格。当图像在完成处理程序块中返回完全加载时(在tableView:cellForRowAtIndexPath :)中我需要再次获取单元格,因为它可能已经滚出视图...所以我调用cellForRowAtIndexPath再次获取单元格 - cellForRowAtIndexPath只返回一个单元格,如果它是可见的。在我的情况下,我从来没有得到它返回任何东西,但没有。即使我滚动速度非常慢(或快速,或者我试过的速度......)我得到的只是零。

我会尽快在这里找到一些代码,但在此之前有任何建议为什么会发生这种情况 - 我猜这块事情会有所提示。

以下是代码:https://dl.dropboxusercontent.com/u/147970342/stackoverflow/appsappsappsappsapps.zip

-tableView:cellForRowAtIndexPath:implementation:

-(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

1 个答案:

答案 0 :(得分:3)

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

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

- (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;
}