Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[self.tableview reloadData]; causes flickering

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
        }


    }];
});
like image 221
user3576169 Avatar asked Apr 26 '14 14:04

user3576169


4 Answers

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

[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
like image 121
Zoeb S Avatar answered Oct 19 '22 20:10

Zoeb S


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

like image 26
NSPratik Avatar answered Oct 19 '22 20:10

NSPratik


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

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

 });
like image 1
Pandey_Laxman Avatar answered Oct 19 '22 19:10

Pandey_Laxman


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

like image 1
Wain Avatar answered Oct 19 '22 19:10

Wain