xcode 仅在 reloadData 完成后调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15218861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:54:20  来源:igfitidea点击:

Call function only after reloadData has finished

iphoneiosobjective-cxcode

提问by Brandon

I have a tableViewand need to perform a function once the tableViewhas been reloaded. How do I know if reloadDatahas finished? Lets say I have methodA that populates the tableView, and once [tableView1 reloadData]has been completed, I want to call methodB. Can anyone help me with this? I've been searching for hours... Thank you!

我有一个tableView并且需要tableView在重新加载后执行一个功能。我怎么知道是否reloadData已经完成?假设我有填充 的方法A tableView,一旦[tableView1 reloadData]完成,我想调用方法B。谁能帮我这个?我已经找了几个小时了……谢谢!

- (void) methodA
{

    NSString *URLa = [NSString stringWithFormat:@"http://www.website.com/page.php?
    v1=%@&v2=%@&v3=%@",v1, v2, v3];

    NSURL *url = [NSURL URLa];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    (void) [[NSURLConnection alloc] initWithRequest:request delegate:self];

    [tableView1 reloadData];

}

回答by Anoop Vaidya

You can add a method/category on UITableView or even subclass UITableView:

您可以在 UITableView 甚至子类 UITableView 上添加方法/类别:

-(void)reloadDataAndWait:(void(^)(void))waitBlock {
    [self reloadData];//if subclassed then super. else use [self.tableView
    if(waitBlock){
        waitBlock();
    }
}

And you need to use it as

你需要把它用作

[self.tableView reloadDataAndWait:^{
    //call the required method here                                            
}];

回答by Martin R

The problem is that you call reloadDataimmediately after starting the URL request. That does not make sense because the request has not been completed at that point.

问题是您reloadData在启动 URL 请求后立即调用。这是没有意义的,因为此时请求尚未完成。

The correct way would be to call reloadDataand methodBin connectionDidFinishLoading, after you have updated your data source with the response from the URL request. At that point you know if the number of rows is zero or not, and there is no need to waitfor the table view update to complete.

正确的方法是在使用 URL 请求的响应更新数据源后调用reloadDatamethodBin connectionDidFinishLoading。此时您就知道行数是否为零,并且无需等待表视图更新完成。

回答by swiftBoy

Once I get updated data from server for my table.

一旦我从服务器为我的表获取更新的数据。

I use following code snip to Hide progress bar when table reload gets complete.

当表重新加载完成时,我使用以下代码片段来隐藏进度条。

[UIView animateWithDuration:0 animations:^{
            [your_table_view reloadData];
        } completion:^(BOOL finished)
        {
            //Table reload completed TODO here
            //Hide progress bar etc.
        }];