Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Parse获取UITableView中的单元格数_Ios_Objective C_Uitableview_Parse Platform - Fatal编程技术网

Ios 使用Parse获取UITableView中的单元格数

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 =

我正在尝试使用Parse检索UITableView中的单元格数

调用return的速度比后台任务完成的速度快,因此单元格数等于零

代码如下:

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

我应该把查询放在哪里?