4

I have a UITableView and I want it so that when you tap on the cell the height animates to a taller height (i.e: it expands). How do I do so?

Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42
adit
  • 32,574
  • 72
  • 229
  • 373

1 Answers1

17

Simple actually, create an instance variable for an NSIndexPath, I've called it selectedIndexPath. Then all you need to do is make a condition in heightForRowAtIndexPath to adjust the height of this specific cell independently from the rest.

Then in didSelectRowAtIndexPath set the selected index path iVar to that of the selected cell, and call begin/end updates on the table. The cell will automatically expand with a nice animation.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectedIndexPath = indexPath;
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([indexPath compare:selectedIndexPath] == NSOrderedSame) {
        return 80;
    }
    return 40;
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281