Ios 从tableView内的块调用CellForRowatineXpath时,CellForRowatineXpath始终为零:CellForRowatineXpath:

Ios 从tableView内的块调用CellForRowatineXpath时,CellForRowatineXpath始终为零:CellForRowatineXpath:,ios,objective-c,uitableview,objective-c-blocks,Ios,Objective C,Uitableview,Objective C Blocks,当从tableView调用CellForRowatineXpath时,CellForRowatineXpath始终返回nil,我有一个问题:CellForRowatineXpath: 问题是我实际上是在tableView内的一个块内调用cellForRowAtIndexPath:cellForRowAtIndexPath:我想这可能就是问题所在 我用远程图像填充单元格,这些图像是临时加载(和缓存)的。当图像在完成处理程序块(tableView:cellforrowatinexpath:内部)中返

当从tableView调用CellForRowatineXpath时,CellForRowatineXpath始终返回nil,我有一个问题:CellForRowatineXpath:

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

我用远程图像填充单元格,这些图像是临时加载(和缓存)的。当图像在完成处理程序块(tableView:cellforrowatinexpath:内部)中返回完全加载时,我需要再次获取该单元格,因为它可能已滚动到视图之外。。。所以我调用cellForRowAtIndexPath再次获取该单元格-cellForRowAtIndexPath只会返回可见的单元格。在我的情况下,我从来没有得到它返回任何东西,除了零。即使我滚动得很慢(或者很快,或者我尝试过的任何速度…),我得到的都是零

我将尝试在这里很快得到一些代码,但在此之前,任何关于为什么会发生这种情况的建议-我想阻塞的事情将是一个提示

代码如下:

-tableView:cellforrowatinexpath:实现:

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

并且需要这个类别:

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

因此,您的实现将如下所示:

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

如果块在
tableView:cellforrowatinexpath:
回调中,则可以直接在块中引用单元格。块将自动保留单元格,直到块消失。但是,请注意保持循环。如果该块属于该单元,则必须使用对该单元的弱引用

因此,您的实现将如下所示:

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

如果块在
tableView:cellforrowatinexpath:
回调中,则可以直接在块中引用单元格。块将自动保留单元格,直到块消失。但是,请注意保持循环。如果该块属于该单元,则必须使用对该单元的弱引用

因此,您的实现将如下所示:

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

如果块在
tableView:cellforrowatinexpath:
回调中,则可以直接在块中引用单元格。块将自动保留单元格,直到块消失。但是,请注意保持循环。如果该块属于该单元,则必须使用对该单元的弱引用

因此,您的实现将如下所示:

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

问题是该单元格无法重用,因为当块返回时,它可能已从视图中滚出。这是一个常见错误,也是需要再次获取单元格的原因。但我会很快发布代码。这可以通过在单元格上运行检查来解决,以确保它仍然表示块刚刚完成下载的相同图像。例如,通过将图像的URL存储在单元格中。听起来我应该下一步尝试。我发布了代码:顺便说一句,这是非常相关的,我只是尝试了你的建议,将一个唯一的值(在我的例子中,我使用应用程序的苹果ID)存储到单元格中,然后在返回时检查它。然后,我不需要使用
cellforrowatinexpath
,我可以像您所说的那样使用单元格的本地引用。到目前为止,这似乎工作得很好。问题是单元格无法重用,因为当块返回时,它可能已从视图中滚动出去。这是一个常见错误,也是需要再次获取单元格的原因。但我会很快发布代码。这可以通过在单元格上运行检查来解决,以确保它仍然表示块刚刚完成下载的相同图像。例如,通过将图像的URL存储在单元格中。听起来我应该下一步尝试。我发布了代码:顺便说一句,这是非常相关的,我只是尝试了你的建议,将一个唯一的值(在我的例子中,我使用应用程序的苹果ID)存储到单元格中,然后在返回时检查它。然后,我不需要使用
cellforrowatinexpath
,我可以像您所说的那样使用单元格的本地引用。到目前为止,这似乎工作得很好。问题是单元格无法重用,因为当块返回时,它可能已从视图中滚动出去。这是一个常见错误,也是需要再次获取单元格的原因。但我会很快发布代码。这可以通过在单元格上运行检查来解决,以确保它仍然表示块刚刚完成下载的相同图像。例如,通过将图像的URL存储在单元格中。听起来我应该下一步尝试。我发布了代码:顺便说一句,这是非常相关的,我只是尝试了你的建议,将一个唯一的值(在我的例子中,我使用应用程序的苹果ID)存储到单元格中,然后在返回时检查它。然后,我不需要使用
cellforrowatinexpath
,我可以像您所说的那样使用单元格的本地引用。到目前为止,这似乎工作得很好。问题是单元格无法重用,因为当块返回时,它可能已从视图中滚动出去。这是一个常见错误,也是需要再次获取单元格的原因。但我会很快发布代码。这可以通过在单元格上运行检查来解决,以确保它仍然表示块刚刚完成下载的相同图像。例如,通过将图像的URL存储在单元格中。听起来我应该下一步尝试。我发布了代码:顺便说一句,这是非常相关的,我只是尝试了你的建议,将一个唯一的值(在我的例子中,我使用应用程序的苹果ID)存储到单元格中,然后在返回时检查它。然后我不需要使用
cellforrowatinexpath
,我可以使用