我与tableview数据源和委托方法斗争。我有一个tableview,如果用户导航到该表视图控制器我调用Web服务方法,这是基于块的服务请求成功完成后重新加载tableview部分,但tableView:cellForRowAtIndexPath:indexPath调用两次。这是我的代码。UITableview委托和数据源调用两次
viewDidLoad中
[[NetworkManager sharedInstance].webService getValues:self.currentId completion:^(NSArray *result, BOOL handleError, NSError *error) {
self.data = result[0];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 4)] withRowAnimation:UITableViewRowAnimationAutomatic];
});
}
但的cellForRowAtIndexPath self.data值是在第一时间空。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%@", self.data); // first time it print null
}
对此有什么想法吗?非常感谢!
你的'UITableView'的dataSource方法将在屏幕加载后被调用,如@Kex建议的,所以它第一次是'null',并且第二次重新加载它的数据将从你的内部触发异步块。 – Levi 2015-02-10 20:18:06
尝试将reloadSections代码放在self.data = result [0]下方;不在调度块中。 – Kex 2015-02-10 20:20:39
@Kex我试了一下。 – 2015-02-10 20:21:52