views:

294

answers:

2

My code looks like this:

NSIndexPath *ip = [NSIndexPath indexPathForRow:15 inSection:0];
[[self tableView] selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionMiddle];

// Pause needed here until animation finishes

[[self navigationController] pushViewController:secondView animated:YES];

Right now it animates the scroll/selection of the row and pushes the view controller at the same time. What I would like it to do is wait until it finishes the scroll/selection and then push the view controller. Is there any possible way of doing this? Thanks

A: 

Is there a reason you don't push the view controller in your -tableView:didSelectRowAtIndexPath: delegate method? That method should be called once the animation is complete.

Martin Gordon
didSelectRowAtIndexPath isn't called at all after selectRowAtIndexPath which is why I'm pushing the view controller separately here
Mike
+1  A: 

The tableview should call scrollViewDidEndScrollingAnimation when the scrolling ends. Try putting the push in that method:

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (weShouldPushSecondView)
    {
        weShouldPushSecondView = NO;
        [[self navigationController] pushViewController:secondView animated:YES];
    }
}

You might need to use a bool ivar that you set to YES right after the selectRowAtIndexPath because the scrollViewDidEndScrollingAnimation may be called at other times for other scrolls.

DyingCactus
This is almost perfect! The only thing it doesn't catch is if the row is already on the screen and therefore doesn't need to scroll to get to it. In this case it only highlights the cell and doesn't call this method. Is there anyway around this?
Mike
Try this: call indexPathsForVisibleRows and see if the cell you are selecting is in the array. If it is, call the push right there (and don't set weShouldPushSecondView to YES). If the cell is not in the visible list, then set weShouldPushSecondView to YES (and don't call the push right there and let it be called when the scroll is done). You probably want to change the name of the bool to pushSecondViewAfterScroll.
DyingCactus