我有一个带有内部表的视图控制器,我想用Parse上保存的数组填充她。要下载数据,我使用以下代码:
PFQuery *query = [PFQuery queryWithClassName:@"myClass"];
[query whereKey:@"X" equalTo:@"Y"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if(error==nil){
myArray=[object objectForKey:@"Z"];
NSLog(@"%@",myArray);
}
}];
}
现在我在myarray
里面显示解析数据。但是,如果我使用数组来填充表格,那么我总是空的。我使用了NSLog
,我在方法[query getFirstObjectInBackgroundWithBlock: ^ (PFObject * object, NSError * error)
之外看到我的数组总是空的。
怎么能帮到我?
答案 0 :(得分:0)
从远程数据库中获取数据需要一些时间。采用块参数的解析函数是异步运行的。请参阅稍加修改的代码中的注释...
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if(error==nil){
// this appears first in the file, but runs later
// after the request is finished
myArray=[object objectForKey:@"Z"];
NSLog(@"%@",myArray);
// tell our view that data is ready
[self.tableView reloadData];
}
}];
// this appears second in the file, but runs right away, right
// when the request is started
// while execution is here, the request isn't done yet
// we would expect myArray to be uninitialized
请确保您的数据源方法,例如numberOfRows
回答myArray.count
。在构建表格视图单元格时使用数组myArray[indexPath.row]
中的数据。