1

シナリオ

parse.com データベースからデータをダウンロードして PFQueryTableViewController に表示するアプリがあります。PFQTVC には複数 (6) のプロトタイプ セルがあり、それぞれに独自の 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に設定しました-問題は、その10番目のセルまでスクロールして次のページをリロードすると...

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

理由

次のページをロードするときself.objects、heightForRowAtIndexPath には、新しいオブジェクトがロードされない 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 に答える