Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate UITableViewCell height? [duplicate]

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?

like image 786
adit Avatar asked Dec 03 '22 00:12

adit


1 Answers

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;
}
like image 117
Mick MacCallum Avatar answered Dec 11 '22 14:12

Mick MacCallum