繁体   English   中英

从tableView:cellForRowAtIndexPath内部的块调用时,cellForRowAtIndexPath始终为零:

[英]cellForRowAtIndexPath always nil when called from block inside tableView:cellForRowAtIndexPath:

我从cellView:cellForRowAtIndexPath调用时,cellForRowAtIndexPath总是返回nil的问题:

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

我正在用临时加载(和缓存)远程图像填充单元。 当图像在完成处理程序块中返回满载时(在tableView:cellForRowAtIndexPath:内部),我需要再次获取该单元格,因为它可能已滚动出视图...所以我调用cellForRowAtIndexPath再次获取该单元格-cellForRowAtIndexPath仅返回一个单元格(如果可见)。 就我而言,除了nil之外,我什么也无法返回。 即使我滚动得非常慢(或快,或我尝试过的任何速度...),我得到的还是零。

我会尽快在这里获取一些代码,但是在那之前,为什么会出现这种情况的任何建议-我想块状的东西都是一个提示。

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

如果该块位于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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM