【问题标题】:Using Parse to get number of Cells in UITableView使用 Parse 获取 UITableView 中的单元格数
【发布时间】:2014-10-12 12:31:17
【问题描述】:

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

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

代码如下:

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

【问题讨论】:

    标签: ios objective-c uitableview parse-platform


    【解决方案1】:

    countObjectsInBackgroundWithBlock 是一种异步方法,它会立即返回并仅在网络调用完成后才运行块,因此numberOfRowsInSection 不会等待异步调用完成

    获取计数的一种方法是使用 @property NSArray *stuff 并以不同的方法从 parse 加载对象,例如 loadStuffFromParse 并在该调用的完成块中执行类似的操作

    我可能有viewWillAppear 中的方法(如果您启用了拉动刷新,则可以在刷新控件操作中挂钩)

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

    或类似的,但你决定。

    numberOfRowsInSection 中假设你只有一个部分可以做

    - (NSInteger)numberOfRowsInSection:(NSInteger) section {
        return [self.stuff count];
    }
    

    【讨论】:

    • 我应该把查询放在哪里?
    猜你喜欢
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多