在后台运行完成后,如何使用parse.com查询中的数据填充UITableView? [英] How can I populate a UITableView with data from a parse.com query after it is done running in the background?

查看:99
本文介绍了在后台运行完成后,如何使用parse.com查询中的数据填充UITableView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableViewController ,负责显示一个满员的表。此数据存储在parse.com上的数据库中。

I have a UITableViewController that is in charge of displaying a table full of employees. This data is being stored on a database on parse.com.

这是我的 UITableViewController ,我只是在其中启动商店:

This is my UITableViewController, in which I just initiate the store:

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nil bundle:nil];
    if(self){
        store = [[EmployeeStore alloc] init]; 
    }
    return self;
}

这是 EmployeeStore init方法,我在其中查询员工:

This is the EmployeeStore init method, in which i query for the employees:

-(id) init{
    self = [super init];
    if(self){
        employees = [[NSMutableArray alloc] init];
        [self fetchEmployeesFromDatabase];
    }
    return self;
}

fetchEmployeesFromDatabase ,其中我查询员工。

fetchEmployeesFromDatabase, where I query for the employees.

-(void) fetchEmployeesFromDatabase{
    PFQuery *query = [PFQuery queryWithClassName:@"Employee"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %d scores.", objects.count);
            // Do something with the found objects
            for (PFObject *object in objects) {
                NSLog(@"%@", object.objectId);
                [employees addObject:object]; 
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

我成功收到它们,但问题是查询发生在后台,并且直到表视图加载完成后才完成,因此表视图不会填充。我需要让表在查询完成后重新加载其数据,但我该怎么做呢?

I am successfully receiving them, however the problem is the query takes place in the background, and does not finish until after the table view is done loading, so the table view does not populate. I need to have the table reload its data after the query is complete but how do I go about doing this?

推荐答案

UITableViewController



UITableViewController

-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nil bundle:nil];
    if(self){
        store = [[EmployeeStore alloc] init];
        store.tableView = self.tableView; 
    }
    return self;
}



EmployeeStore.h



EmployeeStore.h

@property (nonatomic, strong) UITableView *tableView;



EmployeeStore.m



EmployeeStore.m

-(id) init{
    self = [super init];
    if(self){
        employees = [[NSMutableArray alloc] init];
        [self fetchEmployeesFromDatabase];
    }
    return self;
}

和fetchEmployeesFromDatabase

and fetchEmployeesFromDatabase

-(void) fetchEmployeesFromDatabase{
    PFQuery *query = [PFQuery queryWithClassName:@"Employee"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            // The find succeeded.
            NSLog(@"Successfully retrieved %d scores.", objects.count);
            // Do something with the found objects
            for (PFObject *object in objects) {
                NSLog(@"%@", object.objectId);
                [employees addObject:object]; 
            }

             dispatch_async(dispatch_get_main_queue(), ^ {
                [self.tableView reloadData];
              });

        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

这篇关于在后台运行完成后,如何使用parse.com查询中的数据填充UITableView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆