22

Possible Duplicate:
Can you animate a height change on a UITableViewCell when selected?

I have a table view into which where you click on a swich into a cell, many cells of the table view may change their height. It's a little bit complicated to compute which cells will change their size, so I'd like not to call twice the same code.

I have a heightForRowAtIndexPath method (this is the complicated one).

Is there a way to animate the cell grow or reduce feature without impacting the one that does not change ?

I've tried UIView Animations, reloadData for sections with animation, ... but I didn't find anything smooth. I've tried to catch each cell that will have to change and call a reloadData with animation on each cell. It's near to work but the cells are updating one after the other. And as I said before, I don't want to do that because I must call twice the heightForRowAtIndexPath method.

What I'd like is something that can animated each cell that has a changing height with somthing like a grow or reduce movement, and not a fade one.

Do you know a way to do that ? One more thing, the content of the cell is changing a little bit when size change (some text added, back color changing, and things like that)

Community
  • 1
  • 1
Oliver
  • 23,072
  • 33
  • 138
  • 230

1 Answers1

61

UITableView has a neat trick to animate changes in cell heights: if you send an empty updates block to the table view, it won't insert or delete any cells, but it will still recalculate cell heights for the entire table. So even though this piece of code looks weird, it should do it:

[tableView beginUpdates];
[tableView endUpdates];

If you want to change the contents of the updated cells, too, you must add one or more calls to reloadRowsAtIndexPaths:withRowAnimation: to the updates block, of course.

Ole Begemann
  • 135,006
  • 31
  • 278
  • 256
  • That does the job. Could you give me a little last help. I'd like to update all sections so I'm trying to call reloadSections:withRowAnimation. But I'm not used to use NSIndexSet. How would I construct the sections list in that call ? Calling for sure numberOfSectionsInTableView somewhere, but how may I construct the indexSet ? – Oliver Feb 19 '11 at 01:33
  • Say you want to update sections 0 and 3: `NSMutableIndexSet *indexSet = [NSMutableIndexSet indexSet]; [indexSet addIndex:0]; [indexSet addIndex:3];`. See the documentation for more. – Ole Begemann Feb 19 '11 at 01:37
  • That works, thank you, but there are graphic artefacts as I change the background color of the cell. Is there a way to update the content of the cell only when the resizing animation ends ? – Oliver Feb 19 '11 at 01:51
  • seems to be broken on iOS 10 – Sam Oct 07 '16 at 09:59
  • This doesn't work anymore on iOS 16. It has probably been broken since iOS 10. – förschter Jan 26 '23 at 06:29