异步Parse查询和tableView显示

时间:2014-09-28 11:21:09

标签: ios objective-c multithreading uitableview parse-platform

我想用这种查询方法显示来自Parse数据库的数据:

 //Prepare the query to get all the images in descending order
PFQuery *query = [PFQuery queryWithClassName:@"BasketballMatch"];
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {
        //Everything was correct, put the new objects and load the wall
        self.matches = nil;
        self.matches = [[NSArray alloc] initWithArray:objects];
        NSLog(@"Parse push %@", self.matches );
        dispatch_async(dispatch_get_main_queue(), ^ {
            [self.tableView reloadData];
        });

    } else {
        //Show the error
        NSString *errorString = [[error userInfo] objectForKey:@"error"];
        [self showErrorView:errorString];
    }
}];

然后用这个

显示它
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:           (NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"match" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"coba dulu %@", [[self.matches objectAtIndex:indexPath.row] objectForKey:@"TeamName"]];

    return cell;
}

但是表视图单元格将显示第一个

cell1 (null)
cell2 (null)
cell3 (null)
cell4 (null)

然后几秒钟它会显示我想要的显示。

cell1 Soccer
cell1 Tennis
cell3 Basketball
cell4 Badminton

我希望tableview能够立即显示我想要的数据。对此有一个优雅的解决方案......

0 个答案:

没有答案