ios - 在后台运行完成后,如何使用来自 parse.com 查询的数据填充 UITableView?

标签 ios objective-c uitableview parse-platform

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

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

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

这是 EmployeeStore 初始化方法,我在其中查询员工:

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

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]; 
            }
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

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

最佳答案

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

员工商店.h

@property (nonatomic, strong) UITableView *tableView;

员工商店.m

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

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

关于ios - 在后台运行完成后,如何使用来自 parse.com 查询的数据填充 UITableView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17335596/

相关文章:

ios - SWIFT 访问嵌套字典

javascript - iPad 上动态附加的 <style> 不触发 OnLoad 事件

ios - 在 iOS 中将纯文本文档读入 NSArray?

iphone - 单击按钮时如何获取 UITextField 值

ios - 在同一个 UIView 子类上用一根手指滑动和两根手指滑动

ios - 当元素被激活时如何获取indexpath.row?

ios - 如何从 CGPoint 和 CGSize 创建 CGRect?

ios - 程序化语言本地化,无需重启

iphone - 将按钮添加到 UIWebView InputAccessoryView

iphone - 如何唤醒应用程序