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.


Simplicity Defined

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…

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// If our cell is selected, return double height
if([self cellIsSelected:indexPath]) {
return kCellHeight * 2.0;

// Cell isn't selected so return single height
return kCellHeight;


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…

[myTableView beginUpdates];
[myTableView endUpdates];


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…

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;


The Full Code

The full code for the selection method is as follows…

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Deselect cell
[tableView deselectRowAtIndexPath:indexPath animated:TRUE];

// Toggle 'selected' state
BOOL isSelected = ![self cellIsSelected:indexPath];

// Store cell 'selected' state keyed on indexPath
NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
[selectedIndexes setObject:selectedIndex forKey:indexPath];

// This is where magic happens...
[demoTableView beginUpdates];
[demoTableView endUpdates];


So download the demo project and see it in action…. marvellous!


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.

More Posts

So...have we got your attention yet?

Join some of the worlds biggest brands and brightest startups and let us help you bring your ideas to life.

Talk To Us
We are a behaviour-driven agency, crafting creative mobile experiences that engage, retain and convert long-term customers.

Pin It on Pinterest

Share This