Re: Animating a UITableViewCell changing size
Re: Animating a UITableViewCell changing size
- Subject: Re: Animating a UITableViewCell changing size
- From: Luke Hiesterman <email@hidden>
- Date: Thu, 13 Oct 2011 14:37:28 +0000
- Thread-topic: Animating a UITableViewCell changing size
You should never directly set the frame of a cell that is in the table view. Since UITableView automatically requiries and adjusts row heights when you do a begin/endUpdates block, performing an empty such block by itself after adjusting row heights in your model is sufficient to animate a change. The Table View Animations and Gestures sample on developer.apple.com demonstrates this technique coupled with a pinch gesture.
As for the call to reloadRowsAtIndexPaths, that may or may not be appropriate depending on what you're doing. If you truly want to get a given row to animate to a new height, then you don't want to reload it, because that will create a new cell rather than animating the existing one.
In the end, your code could end up looking something like this for animating a row height change:
[self updateModelHeightForRowAtIndexPath:indexPath disclosureOpen:YES];
[tableView beginUpdates];
[tableView endUpdates];
Luke
On Oct 13, 2011, at 2:35 AM, "Thomas Davie" <email@hidden> wrote:
> Dear list,
>
> I'm trying to construct a UITableViewCell with what is essentially a disclosure triangle on it. When that's tapped, the cell expands to show additional information. I currently hack this with code along these lines:
>
> NSArray *cellPaths = [tableView indexPathsForVisibleRows];
> [UIView animateWithDuration:0.3
> animations:^ ()
> {
> MyCustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
> NSUInteger minRow = [indexPath row];
> BOOL d = ![cell disclosed];
> [cell setDisclosed:d];
> [disclosures setObject:[NSNumber numberWithBool:d] forKey:[cell statName]];
> for (NSIndexPath *p in cellPaths)
> {
> if ([p row] > minRow)
> {
> MyCustomCell *c = [tableView cellForRowAtIndexPath:p]
> [c setFrame:CGRectMake([c frame].origin.x, d ? [c frame].origin.y + kDisclosureOffset : [c frame].origin.y - kDisclosureOffset, [c frame].size.width, [c frame].size.height)];
> }
> }
>
> }
> completion:^ (BOOL finished)
> {
> [tableView reloadRowsAtIndexPaths:cellPaths withRowAnimation:UITableViewRowAnimationNone];
> }];
>
> This for the most part works well except for one case – collapsing the cells. When this happens, the table view does not have cells for the rows that are not yet visible, but become visible as the cell collapses. Because of this, a blank area comes into view at the bottom of the table view, and then flashes as it gets replaced by the newly generated rows.
>
> I've tried using cellForRowAtIndexPath: to grab the cells off the bottom, but these come back as nil, as the table view doesn't think it needs to show them. The only other thing I can conjor in my mind is to call something similar to the data source method directly to construct cells myself, add them to the TV, animate them in, remove them and reload the table view. However, this does not solve one final issue – what happens when the table view is scrolled near to the bottom and there are no cells to add.
>
> If anyone could recommend a good way forward here, I'd be appreciative.
>
> Thanks
>
> Tom Davie
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden