简体   繁体   中英

Pushing a view controller after scrolling is complete

When a user adds an item to my list, I want to scroll to the new row, highlight it, and select it (which will push a new controller). The key part is waiting for the scroll animation to complete before pushing the new controller.

In this answer , I learned how to use the animation delegate to wait until the scroll is complete.

However, if the insertion row is already on scree, the table view will not scroll and the method will not fire.

How can I wait to push the new controller until the end of the scroll, and deal with the case where no scroll will be initiated - and how might I tell the difference between each case?

Try creating a method to see if scrolling is needed. If no scrolling is needed, call the push right away, otherwise wait for the delegate call and push.

- (BOOL)isSrollingingNeededForIndexPath:(NSIndexPath *)indexPath {
    NSArray *visibleIndices = [self.tableView indexPathsForVisibleRows];
    for (NSIndexPath *visibleIndexPath in visibleIndices)
        if ([indexPath compare:visibleIndexPath] == NSOrderedSame)
            return NO;
    return YES;
}

Edit: Good point. Since indexPathsForVisibleRows is used for data rendering.

You could do essentially the same thing with indexPathsForRowsInRect where you use the content.offset.y and the tableview.frame.size.height to determine your "visible rect".

Then to account for partially visible rows at the top and bottom you could add rowHeight-1 to the top of the rect and subtract rowHeight - 1 from the bottom of the rect. Code shouldn't be too gnarly if you have static height rows. If you have varying height rows it would still work, but it would be a bit more involved.

All said though, it seems like a lot of code for something which you'd think would have a simple answer.

The easiest way to check whether a given row is visible in your table view is something like this:

if (!CGRectContainsRect([self.tableView bounds], [self.tableView rectForRowAtIndexPath:indexPath])
{
    // the row is partially outside the table view’s boundaries and needs to be scrolled for full visibility
}
else
{
    // the row is within the boundaries and does not need to be scrolled
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM