[self.tableview reloadData]; causes flickering

2019-04-09 00:20发布

The problem is the UI appears and then gets updated : giving a flickering affect.

I want the UI to be updated only once when user enters app, thus i've put reload in ViewDidLoad.. Here is the code .. Any help how can remove this flickering ... Some code example would help.

- (void)viewDidLoad { 

[super viewDidLoad];

self.myTableView.dataSource = self;
self.myTableView.delegate = self;

PFQuery * getCollectionInfo = [PFQuery queryWithClassName:@"Collection"];   // make query

[getCollectionInfo orderByDescending:@"updatedAt"];
[getCollectionInfo setCachePolicy:kPFCachePolicyCacheThenNetwork];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    [getCollectionInfo findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            CollectionQueryResult = (NSMutableArray *)objects;
                [self.tableView reloadData];


            // whenevr get result
        }
        else{
            //no errors
        }


    }];
});

4条回答
等我变得足够好
2楼-- · 2019-04-09 00:58

Your download is asynchronous, so it won't complete before the view is shown. So, whatever you have in CollectionQueryResult will get displayed.

You could either:

  1. Clear CollectionQueryResult so the table isn't populated till you get an update
  2. Compare CollectionQueryResult and objects, then update only the visible cells where the data has changed (before setting CollectionQueryResult)

Note that for option 2 you will also need to compare the count of CollectionQueryResult and objects and then insert / delete rows from the table view as appropriate. The whole point of option 2 is to not call reloadData, so you need to do a lot more work...

You can also avoid calling reloadRowsAtIndexPaths if you get the visible cells and update them directly (which avoids any animation from happening). See cellForRowAtIndexPath:.

查看更多
走好不送
3楼-- · 2019-04-09 01:06

Refer cellForRowAtIndexPath delegate method. You might be doing some operation that might cause a flick. In my case, I was setting an image of an image view with animation. So whenever I reload the table, it was flickering due to that animation.

I removed that animation and worked for me..

查看更多
在下西门庆
4楼-- · 2019-04-09 01:06

Hope the below lines can help you in delaying and flickering issues

dispatch_async(dispatch_get_main_queue()
                   , ^{
 [self.tableView reloadData];

 });
查看更多
孤傲高冷的网名
5楼-- · 2019-04-09 01:21

Why don't you simply call reloadSections method instead of [self.tableView reloadData];

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
查看更多
登录 后发表回答