我在 ViewDidLoad 中使用 AFNetworking GET 方法。我的 UITableView 委托(delegate)方法在数据加载到数组之前运行。我收到 NSArray beyond bounds 的错误。
请帮助我完成它。这是我第一次使用 JSON。
我搜索了 Stackoverflow 和谷歌。但没有得到正确的答案。
最佳答案
如果您不希望它从空白数组中提取数据,那么您不应该在下载完成之前刷新您的 TableView 。
您应该在 AFNetworking
调用的成功 block 中刷新数据。
[connectionMgr GET:@"yourURL" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { //Note that 204 is considered a success message
//Reload your table view
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { //Note that this is called even if the download is cancelled manually
//Failure
}];
编辑
由于您使用的是 UITableViewController
,因此您应该检查 numberOfRowsInSection
以查看数组是否为 nil
或者它是否为包含 0
个对象。然后它不会尝试生成任何单元格。
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (array == nil || array.count < 1) {
return 0;
} else {
return array.count; //Or whatever you're using
}
}
我假设您没有使用 array.count
作为单元格数量,否则您可能不会遇到此问题。
关于ios - 如何在从 JSON 加载数据之前停止运行委托(delegate)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25322018/