NERD ALERT! This is a tech article, the faint of heart should browse on…
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…
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…
What this results in is the UITableView re-evaluating it’s visible cells and setting their size accordingly but without reloading the data, and the best part? IT ANIMATES IT!
In the demo project I toggle the selected status of a cell and call these magic two lines in the following method…
The Full Code
The full code for the selection method is as follows…
So download the demo project and see it in action…. marvellous!
|Custom AlertView (0)||2012/04/29|
|How does UITableViewCell reorder, expand/shrink work ? (0)||2012/04/28|
|Animate UITableView Cell Height Change (0)||2012/04/28|
|TextField 특정 문자만 사용하도록 하기 (0)||2012/04/28|
|objective c 수학함수 (0)||2012/04/28|
|iPhone UITableView with animated expanding cells tutorial (0)||2012/04/26|