ios - 如何在从 JSON 加载数据之前停止运行委托(delegate)方法?

标签 ios uitableview afnetworking

我在 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/

相关文章:

ios - UITableViewController - 图像背景

ios - 从以编程方式创建的导航栏项设置操作

ios - 如何使用 AFNetworking 允许 https 连接?

ios - `scene(_ scene: UIScene, continue userActivity: NSUserActivity)` 用户点击通用链接后启动应用时不被调用

ios - UITableViewCell 宽度不根据 ios 设备类型动态调整

iOS/swift : Two embedded views in UITableViewCell not appearing when run

ios - 如何在 Safari 上制作直播 swf 广播

ios - UIImageView+AFNetworking maxConcurrentOperationCount

ios - AFHTTPClient 将授权 header 字段设置为 "Token token='\75845hjhrtje84539574 8' "但我只想成为里面的 token

ios - AFNetworking - 获取响应内容长度