1

设想

我有一个应用程序,它从我的 parse.com 数据库下载数据并将其显示到 PFQueryTableViewController 中。PFQTVC 有多个 (6) 原型单元,每个单元都有自己的重用标识符,最重要的是,它们自己的高度。我目前正在使用以下设计为每个indexPath.row...找到正确的高度

目前

这在启用分页之前有效

每个单元格都是独一无二的,因为每个单元格都显示不同类型的信息。所以我正在检查每self.objects一个他们拥有的每一个独特的品质。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

                        // \!/ -trouble maker 
    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    NSString *orientation = object[@"orientation"];

    NSNumber *height;

    if ([orientation  isEqual: @"left"] || [orientation  isEqual: @"right"]) {

        height = @400.0;
    }

    else if ([orientation  isEqual: @"up"] || [orientation  isEqual: @"down"]) {

        height = @500.0;
    }

    else if (blah blah blah) {

        //ect.
    }

    return [height floatValue];
} 

这一切都有效。也就是说,在我启用分页之前。

当我启用分页时

像大多数应用程序一样,我的应用程序需要能够对数据进行分页,而这正是我的工作。我将我的设置objectsPerPage为 10 -问题是,当我向下滚动到第十个单元格并重新加载下一页时......

...reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'

原因

当它加载下一页时,self.objectsheightForRowAtIndexPath 是一个 NSArray,它不会加载新对象。这导致我的应用程序崩溃并使我非常沮丧。

问题

有谁知道如何解决这个问题?

编辑

NSNumber *height;

if (indexPath.row > self.objects.count) {

    height = @50.0;
}

else {

    PFObject *object = [self.objects objectAtIndex:indexPath.row];

    NSString *orientation = object[@"orientation"];

    if ([orientation  isEqual: @"left"] || [orientation  isEqual: @"right"]) {
        //HAS COMMENTS
        if ([[object objectForKey:@"comments"]intValue] > 0) {


            height = @618.0;

        }

        else {


            //NO COMMENTS BUT HAS LIKES
            if ([[object objectForKey:@"likes"]count] > 0) {


                height = @398.0;

            }
            //HAS NOTHING
            else {


                height = @381.0;

            }

        }

    }

    else if ([orientation  isEqual: @"up"] || [orientation  isEqual: @"down"]) {

        if ([[object objectForKey:@"comments"]intValue] > 0) {


            height = @727.0;

        }

        else {


            if ([[object objectForKey:@"likes"]count] > 0) {


                height = @508.0;

            }

            else {

                height = @490.0;

            }

        }

    }

    return [height floatValue];

}
4

1 回答 1

1

启用分页后,Parse 在第 11 个单元格处添加一个“加载更多”单元格。不过 self.objects 中没有第 11 个对象。您需要检查 indexPath.row < self.objects.count 并为 Load More 单元格返回不同的高度。

这应该可以防止该错误:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // \!/ -trouble maker
    if (indexPath.row== self.objects.count) return 40.0;

    PFObject *object = [self.objects objectAtIndex:indexPath.row];
    NSString *orientation = object[@"orientation"];

    NSNumber *height;

    if ([orientation  isEqual: @"left"] || [orientation  isEqual: @"right"]) {

        height = @400.0;
    }

    else if ([orientation  isEqual: @"up"] || [orientation  isEqual: @"down"]) {

        height = @500.0;
    }

    else if (blah blah blah) {

        //ect.
    }

    return [height floatValue];
}

当我为 loadNextPage 实现轻微延迟时,它工作得很好。必须有一个方法在正确加载之前尝试访问 self.objects 数组。

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row >= self.objects.count)
    {
        // this method gets called when the cell is scrolling into view, but also when it's first added to the table view
        // we only care about the first case
        if ([tableView.indexPathsForVisibleRows containsObject:indexPath])
        {
            [self performSelector:@selector(loadNextPage) withObject:nil afterDelay:.2];
        }
    }
}
于 2014-10-19T23:43:40.250 回答