简体   繁体   English

仅在reloadData完成后才调用函数

[英]Call function only after reloadData has finished

I have a tableView and need to perform a function once the tableView has been reloaded. 我有一个tableView和需要,一旦执行功能tableView已重新加载。 How do I know if reloadData has finished? 我如何知道reloadData是否已完成? Lets say I have methodA that populates the tableView , and once [tableView1 reloadData] has been completed, I want to call methodB. 假设我有填充tableView ,一旦[tableView1 reloadData]完成后,我想调用methodB。 Can anyone help me with this? 谁能帮我这个? I've been searching for hours... Thank you! 我一直在寻找时间...谢谢!

- (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];

}

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                                            
}];

The problem is that you call reloadData immediately after starting the URL request. 问题是您在启动URL请求后立即调用reloadData That does not make sense because the request has not been completed at that point. 这是没有意义的,因为此时请求尚未完成。

The correct way would be to call reloadData and methodB in connectionDidFinishLoading , after you have updated your data source with the response from the URL request. 正确的方法是使用URL请求的响应更新数据源后,在connectionDidFinishLoading调用reloadDatamethodB At that point you know if the number of rows is zero or not, and there is no need to wait for the table view update to complete. 届时,您将知道行数是否为零,并且无需等待表视图更新完成。

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.
        }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM