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?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With