trillions
trillions

Reputation: 3709

reload uitableview with new data caused flickering

I added an infinite scrolling feature and realized that whenever I reload the uitableview, the view flickers..I am not sure how to fix the flickering at this point. Please help, thanks!

I did see a very similar question, but no solution to it. I tried the one the author posted, but it doesn't not work: "remove the tableview from parent view, reload data, put table view back into parent view" (UITableView reloadData - how to stop flicker)

Code:

[self.tableView reloadData];

Upvotes: 25

Views: 36511

Answers (9)

Abhijeet Ravi
Abhijeet Ravi

Reputation: 295

Try this:

UIView.performWithoutAnimation {
    self.tableView.reloadRows(at: [indexPath], with: .none)
}

Upvotes: 3

Shaunak
Shaunak

Reputation: 717

Below code worked for me like a charm!

Objective-C

[UIView performWithoutAnimation:^{
   [self.tableview reloadData];
   [self.tableview beginUpdates];
   [self.tableview endUpdates];
}];

Swift 4

UIView.performWithoutAnimation {
    self.tableView.reloadData()
    self.tableView.beginUpdates()
    self.tableView.endUpdates()
}

Upvotes: 28

Moss
Moss

Reputation: 234

As you have guessed, flickering is caused by calling [self.tableView reloadData]; rapidly and repeatedly, especially on iOS 5.x devices. But you probably do not want to reload the entire table, you want to update just the view within the visible cells of the table.

Let's say you want to update each cell of a table to reflect the latest download % as a file is downloading. A method [downloading:totalRead:totalExpected:] gets called in my example, extremely rapidly as bytes are downloading.

This is what NOT to do... reload the table on every little update (in this example, the developer may rely on "cellForRowAtIndexPath" or "willDisplayCell" methods perform the updating of all the visible cells):

    - (void)downloading:(PPFile *)file totalRead:(long long)read totalExpected:(long long)expected {
        // Bad! Causes flickering when rapidly executed:
        [self.tableView reloadData];
    }

The following is a better solution. When a cell's view needs to be updated, find that cell by grabbing only the visible cells from the table, and then update that cell's view directly without reloading the table:

    - (void)downloading:(PPFile *)file totalRead:(long long)read totalExpected:(long long)expected {
        NSArray *cells = [self.tableView visibleCells];

        for(MYCellView *cell in cells) {
            if(cell.fileReference == file) {
                // Update your cell's view here.
            }
        }
    }

EDIT: The docs recommend the same:

Calling this method causes the table view to ask its data source for new cells for the specified sections. The table view animates the insertion of new cells in as it animates the old cells out. Call this method if you want to alert the user that the values of the designated sections are changing. If, however, you just want to change values in cells of the specified sections without alerting the user, you can get those cells and directly set their new values.

Upvotes: 16

gal danay
gal danay

Reputation: 11

what work in my project was :

UIView.PerformWithoutAnimation(() =>
{
tableView.ReloadData();
tableView.LayoutIfNeeded();
});

(it's in c# xamarin...but the same with swift of objective c)

Upvotes: 1

Yam
Yam

Reputation: 793

Just in case all other answers do not solve your problem, I post my solution.

If you use estimated height estimatedSectionHeaderHeight or estimatedRowHeight or -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath, and with dynamic row count for each section (e.g. expandable two level tableView)

you should stop using this and implement -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath to return the correct height.

Upvotes: 2

NSPratik
NSPratik

Reputation: 4846

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

I removed it and worked for me..

Upvotes: 1

Zoeb S
Zoeb S

Reputation: 705

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

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

Upvotes: 3

Durgaprasad
Durgaprasad

Reputation: 1951

y you are using deleteRowsAtIndex? If you reload table all table delegate methods are called. I think it is taking time to delete all rows and then again reload. Just use reload method and try.

Upvotes: 0

Shubhank
Shubhank

Reputation: 21805

    [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationNone];

whenever you remove or add a table row in updates..the table view reloads and ask the datasource for the number of rows based on which cell is animated to position.

you are removing row from table but not from datasource..so rows are deleted but your datasource still points out that no row is deleted..20 objects are still there. your data source will be something like

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return MyDataModel.count; // 
}

so you need to remove or add new data in datasource also..something like this

   NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
   [MyDataModel removeObjectAtIndex:index.row];
   [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:index] withRowAnimation:UITableViewRowAnimationNone];

Upvotes: 3

Related Questions