UIRefreshControl: UITableView 'stuck' while refreshing

柔情痞子 提交于 2019-12-04 14:04:59

问题


I'm having a problem implementing UIRefreshControl - in that when you pull down, the 'blob' works perfectly fine and the refresh spinner works fine, but the tableView doesn't scroll up to the spinner whilst refreshing. Instead, it stays where it was until the refreshing is complete, at which point it returns to the top of the screen

The code that does the refreshing is:

- (void)viewDidLoad {
    self.refreshControl = [[UIRefreshControl alloc] init];
    [self.refreshControl addTarget:self action:@selector(refreshView:)forControlEvents:UIControlEventValueChanged];
}

- (void)refreshView:(UIRefreshControl *)refresh  {
    dispatch_async(dispatch_get_main_queue(), ^{
        (...code to get new data here...)
        [self.refreshControl endRefreshing];
    }
}

I found that without dispatch_async, even the refresh spinner doesn't work - and the bit that was pulled down appears just white

Does anyone have any clues what I could be doing wrong? All implementation examples I've found seems to match what I'm doing, and I haven't found anything in the API docs that suggest I'm missing anything out


回答1:


You can change to following

- (void)refreshView:(UIRefreshControl *)refresh  {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // (...code to get new data here...)
        dispatch_async(dispatch_get_main_queue(), ^{
            //any UI refresh
            [self.refreshControl endRefreshing];
        });
    });
}

-refreshView: will get called on the main thread, and all UI updates are using the main thread. So if you use the main thread for "code to get new data" it will "stuck"



来源:https://stackoverflow.com/questions/13387378/uirefreshcontrol-uitableview-stuck-while-refreshing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!