UIRefreshControl: UITableView 'stuck' whil

2019-03-21 07:01发布

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条回答
forever°为你锁心
2楼-- · 2019-03-21 07:35

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"

查看更多
登录 后发表回答