[self.tableview reloadData]; causes flickering

2019-04-09 00:53发布

问题:

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
        }


    }];
});

回答1:

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

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];


回答2:

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..



回答3:

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

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

 });


回答4:

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:.