0

以下は解析クエリです。コレクション ビューにカテゴリを表示しようとしていますが、解析から情報を取得numberOfItemsInSectionする前にメソッドが実行getCategoriesされます。numberOfItemsInSectionを使用getCategories _anArrayOfCategoriesして、コレクション ビュー内のカテゴリの数を返します。

-(void)getCategories{
    [super viewWillAppear:animated];   //calls retrieve messages method below

    //get Categories where the class name is Categories
    PFQuery *query = [PFQuery queryWithClassName:@"Categories"];
    //- (void)selectKeys:(NSArray *)keys
    [query selectKeys:@[@"CName"]];
    //[query whereKey:@"recipientIds" equalTo:[[PFUser currentUser] objectId]];
    [query orderByAscending:@"createdAt"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }

        else {
            _anArrayOfCategories = [[NSArray alloc] initWithArray:objects];
            NSLog(@"Test 1: Retrieved %lu Categories", (unsigned long)[_anArrayOfCategories count]);

        }
    }];

}

助言がありますか?

4

1 に答える 1

1

クエリの完了後にテーブルを更新するか、前のコントローラーでクエリを作成し、完了後にこのコントローラーにプッシュします。これらは UI 操作であり、メイン スレッドで実行する必要があることに注意してください。

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (error) {
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }

    else {
        _anArrayOfCategories = [[NSArray alloc] initWithArray:objects];
        NSLog(@"Test 1: Retrieved %lu Categories", (unsigned long)[_anArrayOfCategories count]);
        dispatch_async(dispatch_get_main_queue(), ^{ // UI operations on the main thread
            [self.tableView reloadData];
        });

    }
}];

編集:念のため、あなたの投稿からは不明であるため、からこのメソッドを呼び出したくありませんnumberOfRowsInSectionviewDidLoadそれを入れたり、似たようなものを入れたりしてから、オブジェクトをnumberOfRowsInSection使用してください。_anArrayOfCategories

于 2014-06-11T20:58:39.770 に答える