Call function only after reloadData has finished

前端 未结 3 1195
挽巷
挽巷 2021-01-07 13:56

I have a tableView and need to perform a function once the tableView has been reloaded. How do I know if reloadData has finished? Lets

相关标签:
3条回答
  • 2021-01-07 14:43

    The problem is that you call reloadData immediately after starting the URL request. 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. 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.

    0 讨论(0)
  • 2021-01-07 14:57

    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.
            }];
    
    0 讨论(0)
  • 2021-01-07 15:01

    You can add a method/category on UITableView or even subclass 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                                            
    }];
    
    0 讨论(0)
提交回复
热议问题