views:

303

answers:

1

Hi

I'm inserting a new cell into my table that I know will not be visible (I know it's off the bottom of the screen), so I then call scrollToRowAtIndexPath, e.g.

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:myIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
[tableView scrollToRowAtIndexPath:myIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];

However, when I do this I can actually see the cell getting drawn/animated twice, and removing the UITableViewRowAnimationBottom from the insert would mean that it would not be animated when the insertion should be visible (e.g. at the 2nd row).

Any ideas how I can fix this animation issue?

Thanks,
Paul

+1  A: 

You could check whether the row is visible-- if visible, the skip the scroll, else use UITableViewRowAnimationNone. If you call UITableView's -cellForRowAtIndexPath: , it should return nil if the row is not visible.

Paul Collins
The problem I have with doing that is that I cannot figure-out if the newly inserted cell will be visible until after it's been inserted, and therefore although I can decide whether or not to scroll it is too late to make the decision about wither to insert the row using UITableViewRowAnimationBottom or UITableViewRowAnimationNone. Have I missed something obvious here?
Paul
I should add that calling UITableView's -cellForRowAtIndexPath with the projected indexPath does not seem to return nil if the cell is not visible (at least not for me, but maybe the fact that I have my table split into sections complicates this).
Paul
In that case how about -indexPathsForVisibleRows ? Unless you're appending to the bottom, there is already a row at myIndexPath that is visible if your insert will be visible, not visible if it won't. I think. I haven't dealt with this--my inserts always happen offscreen because I'm jumping to a detail view.
Paul Collins
Although I can work out if the cell will be visible after insertion which will allow me to decide on whether I need to auto-scroll, the redraw issue is caused by the actual animated insertion and I still can't figure-out how to predict correctly if it will be visible (different rows have different heights, so even trying to work out the on-screen coordinates is difficult, although I may investigate).For now I have improve the redraw by calling 'reloadRowsAtIndexPaths' after auto-scrolling. This improves the animation, but is not perfect. Thanks.
Paul