iOS NSURLConnection 不调用 connectionDidFinishLoading

iOS NSURLConnection doesn't call connectionDidFinishLoading

我有一个 UIViewControllerUITableView

在我的 viewDidLoad 方法中,我调用 NSURLConnection:[[NSURLConnection alloc]initWithRequest:request delegate:self]

在此之后,我假设应该调用 NSURLConnectionDelegate 方法,例如 didReceiveResponse。我的想法是获取数据并用它填充table。

但实际上,在 NSURLConnection 调用之后,UITableViewDataSource 方法在 didReceiveResponse 方法之前被调用。因此,我无法使用新数据初始化 table 视图。

为什么?如何解决?

非常感谢!

您可以通过在收到新信息后使用 UITableView 的 reloadData 方法或在特定索引处插入新行来解决此问题,这样整个 UITableView 就不会更新。

假设您有一个数组,其中包含您必须在 UITableView 上显示的元素

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Update data from the response to your elements array
    ...

    // Reload Data
    [self.tableView reloadData];
}