17

I have a simple UITableView with UITableViewCells. For the 0th row in the tableview, I have set a background color for the cell. Now on tapping the 0th row I just want the row to expand. In order to achieve this, I'm using the same technique as described here to change the 0th row height.

Now the problem is that the cell expands and collapses, but with each process there's an ugly flash/flicker on the cell. I've tried both beginupdates/endupdates and also reloading just that single row using [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationNone];

What's causing the flicker/flash on tapping? And how do I get rid of it? And yes, I forgot to mention, I have set the cell's selection style to none. So that's not causing this.

Community
  • 1
  • 1
Bourne
  • 10,094
  • 5
  • 24
  • 51

6 Answers6

5

I had the same issue when using non-opaque background color of the cell. The solution is to use backgroundView property, instead of backgroundColor:

UIView *backgroundView = [[UIView alloc] initWithFrame:cell.frame];
backgroundView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1f];
cell.backgroundView = backgroundView;
cell.backgroundColor = [UIColor clearColor];
Karol Kulesza
  • 909
  • 8
  • 10
4

I've had a similar issue, with a few culprits:

  • Make sure all your tableview calls are on the main thread.
  • Check your cell height logic, if there are inconsistencies it could cause similar issues. Remember the cell heights need to be updated to the final heights before you ask the table to reload rows.
  • Use a solid cell background color. iOS 7 seems to have issues if the cell background color is not fully opaque. (I couldn't find a solution to this while while using a non-opaque bg color. I suspect it may be an iOS 7 bug.)
psilencer
  • 79
  • 8
  • 1
    What do you mean by this? Remember the cell heights need to be updated to the final heights before you ask the table to reload rows. – msfeldstein Aug 08 '15 at 04:46
  • I think I meant that you need to make sure your delegate is set up to return the new hight before you call [tableview beginUpdates]. – psilencer May 09 '16 at 20:08
  • As of iOS 10, using a non-opaque background color for the cell still causes flickering issues when adding or removing rows. – phatmann Sep 27 '16 at 12:51
1

If you don't want animation and stop the cell from flashing, you can use following solution -

     [UIView setAnimationsEnabled:NO];
     [self.tableView beginUpdates];

     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil]
                                               withRowAnimation:UITableViewRowAnimationNone];
     [self.tableView endUpdates];
     [UIView setAnimationsEnabled:YES];

The above snippet will have no animation on cells while reloading the cell

Sandy
  • 3,021
  • 1
  • 21
  • 29
0

I think you should not use beginUpdate/endUpdates methods and perform animations manually before calling reloadData. The final state of cells (after animation) are defined with datasource. I hope this will helps you.

Irfan
  • 4,301
  • 6
  • 29
  • 46
  • I'm not using beginUpdate/endUpdate (though that also caused issues as I've mentioned in the question). I'm using reloadrowsat... to just reload that particular row. – Bourne Feb 25 '14 at 09:25
  • Then I suggest you to apply simple animation may be this will resolve your problem like this: [UIView animateWithDuration:1.0 animations:^{ [self.tableView setContentOffset:CGPointMake(0, newDistance) animated:NO]; [self.tableView setRowHeight:newHeight]; }]; – Irfan Feb 25 '14 at 09:32
  • 1
    Or I suggest that you can add a background view into your UITableViewCell with a backgroundColor. Then in your tableView: didSelectRowAtIndexPath: you can animate the hidden or alpha with the animation APIs in UIView. When the animation finishes you can animate hidden to TRUE or alpha back to 0 to get the flash effect – Irfan Feb 25 '14 at 09:35
0

I'm not sure if this is the same problem as I faced today morning but my solution may help somebody...

My subviews disappeared during reloading with animation... It didn't mattter if I used

[self.tableView beginUpdates];
[self.tableView endUpdates];

or

[self.tableView reloadRowsAtIndexPaths:]

Finally I found out that I modified frames of the subviews of the cell for calculating the proper sizes... I changed it and started using other views for calculations and it solved my problem.

Instead of:

self.leftLabel.frame = CGRectMake(0, 0, innerWidth, CGFLOAT_MAX);
[self.leftLabel sizeToFit];

I made calculations this way:

UILabel * tmpLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, innerWidth, CGFLOAT_MAX)];
tmpLabel.numberOfLines = self.leftLabel.numberOfLines;
tmpLabel.attributedText = self.leftLabel.attributedText;
[tmpLabel sizeToFit];

self.leftLabel.frame = tmpLabel.frame;
Marek Manduch
  • 2,303
  • 1
  • 19
  • 22
-1

I would expect the flash because you've chosen no animation, i.e., UITableViewRowAnimationNone. Try the fade animation.

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade]];

If that still doesn't look good, try animating all the visible rows:

[self.tableView reloadRowsAtIndexPaths:[self.tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationFade]];
bilobatum
  • 8,918
  • 6
  • 36
  • 50
  • It still flashes. Regardless of the animation choice, or doing it just for that row or visible rows. – Bourne Feb 25 '14 at 09:24