Ios 使用Parse获取UITableView中的单元格数
我正在尝试使用Parse检索UITableView中的单元格数 调用return的速度比后台任务完成的速度快,因此单元格数等于零 代码如下:Ios 使用Parse获取UITableView中的单元格数,ios,objective-c,uitableview,parse-platform,Ios,Objective C,Uitableview,Parse Platform,我正在尝试使用Parse检索UITableView中的单元格数 调用return的速度比后台任务完成的速度快,因此单元格数等于零 代码如下: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { #warning Incomplete method implementation. __block int rows; PFQuery *query =
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
__block int rows;
PFQuery *query = [PFQuery queryWithClassName:@"bills"];
[query whereKey:@"houseId" equalTo:@1];
[query countObjectsInBackgroundWithBlock:^(int count, NSError *error) {
if (!error) {
rows = count;
NSLog(@"in block = %d",rows);
} else {
rows = 0;
}
}];
NSLog(@"out of block = %d",rows);
return rows;
}
countObjectsInBackgroundWithBlock
是一个异步方法,它立即返回,并且只在网络调用完成后运行块,因此numberOfRowsInSection
不会等待异步调用完成
获取计数的一种方法是使用一个@属性NSArray*stuff
并以另一种方法从parse加载对象,例如,loadStuffFromParse
,并在该调用的完成块中执行以下操作
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self loadStuffFromParse];
}
我可能会将视图中的方法显示为(如果启用了“拉入刷新”功能,则会在刷新控件操作中挂接该方法)
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self loadStuffFromParse];
}
吃点像这样的东西
#pragma mark - Implementation
- (void)loadStuffFromParse {
// Do anything we need to do before loading like updating user interface
// [PFQuery loadStuffFromParse:^(NSArray *data, NSError *err ){
// if (!err) {
// self.stuff = data; // defined in @interface
// dispatch_async(dispatch_get_main_queue(), ^{
// [self.tableView reloadData];
// });
// } else {
// handle the error
// }
//}];
}
或者诸如此类的事情,不管你怎么决定
在numberofrowsin部分
中,假设您只有一个部分可以执行
- (NSInteger)numberOfRowsInSection:(NSInteger) section {
return [self.stuff count];
}
我应该把查询放在哪里?