ios - 解析iOS SDK : Dynamic cell height for PFQueryTableViewController based on results from Query

标签 ios objective-c parse-platform heightforrowatindexpath

场景

我有一个应用程序可以从我的 parse.com 数据库下载数据并将其显示到 PFQueryTableViewController 中。 PFQTVC 有多个 (6) 个原型(prototype)单元格,每个原型(prototype)单元格都有自己的 reuseIdentifier,最重要的是,还有自己的高度。我目前正在使用以下设计为每个 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]'

原因

当加载下一页时,heightForRowAtIndexPath 中的 self.objects 是一个 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];

}

最佳答案

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

这应该可以防止该错误:

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

关于ios - 解析iOS SDK : Dynamic cell height for PFQueryTableViewController based on results from Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26456512/

相关文章:

ios - 解析云代码推送通知

javascript - Parse.com 云错误请求 400

ios - Swift:在向 UIImageView 添加边框时获取小条

ios - 如何检索选定单元格的集合

ios - 在 App Store 中发布之前测试 Facebook iOS 请求?

ios - 在 .xib 文件中对 UIView 进行动画处理

iphone - 何时何地放置@class 声明

iphone - 在 Cocos2d 中更改多个 Sprite 的不透明度

ios - 如何使用collectionview制作这个垂直的coverflow动画

ios - 最终使用 PFFile(解析本地数据存储)保存在 PFObject 上?