6

我有一个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;
}

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

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

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

4

5 回答 5

11

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;

EmployeeStore.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]);
        }
    }];
}
于 2013-06-27T06:17:12.327 回答
2

当加载完成时,告诉表视图重新加载。

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(), ^ {
        [tableView reloadData];
    });
} else {

您需要确保您的员工存储是线程安全的,或者不会被后台和主线程同时访问。

于 2013-06-27T06:06:41.503 回答
0
-(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]);
    }
    [self performSelectorOnMainThread:@selector(reloadTableView) withObject:nil waitUntilDone:NO];
}];
}

-(void)reloadTableView{
    [self.aTableview reloadData];
}

现在你在主线程上加载你的表视图。尝试这个。

于 2013-06-27T06:15:34.387 回答
0

我遇到了这个问题,除了集合视图。在经历了很多痛苦之后,这可以通过主线程重新加载数据。

dispatch_async(dispatch_get_main_queue(), ^ {
            [self.collectionView reloadData];
            [self.collectionView reloadItemsAtIndexPaths:[self.collectionView   indexPathsForVisibleItems]];
        });
于 2014-02-17T06:19:53.080 回答
0

在这里,数据甚至在表格视图完成之前就被填充到表格中。您需要在这里做的就是放置语句 [self fetchEmployeesFromDatabase]; 在 viewDidLoad() 方法或调用您的表创建的任何方法中。

于 2013-06-27T06:46:17.890 回答