Well this is going to be a fairly short article, but I just wanted to share with you a snippet of magic I discovered quite some time ago. It is something that I posted on StackOverflow and to date is the most up-votes I have received on there, with votes coming in pretty much every day.
The magic is in how you can animate the height change for a UITableViewCell in a normal UITableView.
The source code for this article can be downloaded here.
This really is as simple as it gets. First you need to implement all of the standard methods that you are required to for a UITableView datasource and delegate. But the key one to look at is the following…
// Cell isn’t selected so return single height
What this does is check whether the cell is currently selected and if so returns double the normal height. kCellHeight is a compiler define and cellIsSelected: is a method which takes an indexPath and returns whether the cell is selected or not. This doesn’t come from the cell itself, instead I am just storing the selected state in an NSMutableDictionary keyed on the indexPath.
Animate The Change
So we know that returning a variable height in the heightForRowAtIndexPath method will result in the table sizing the cells accordingly, but how do we animate the change… well simplicity itself… you simply use these two lines…
Simon has worked in the software industry for over 20 years; intent on always producing work of the highest standard and creating software products that genuinely makes things better for people. Simon has previously held positions ranging from Developer, Technical Consultant, Head of Development through to CTO and more recently founder and CEO of several high profile technology companies.