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

    }
}];

编辑:只是为了确保,由于您的帖子不清楚,您不想从numberOfRowsInSection. 将其放入viewDidLoad或类似的东西中,然后numberOfRowsInSection使用该_anArrayOfCategories对象。

于 2014-06-11T20:58:39.770 回答