呈现另一个视图控制器将UITableView滚动到顶部

时间:2013-10-17 00:59:58

标签: ios objective-c uitableview

我有一个PFQueryTableViewController,设置为在选择行时在UINavigationViewController内实例化并显示一个新的视图控制器。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self objectAtIndexPath:indexPath]) { // Check our object exists
        MyNewViewController *card = [[MyNewViewController alloc] init];

        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:card];
        [nav.navigationBar configureFlatNavigationBarWithColor:[UIColor pomegranateColor]];
        [self presentViewController:nav animated:YES completion:^{}];
    }
    else { // Otherwise, do what we would have done.
        [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}

当我选择一行时,表视图会在显示新视图控制器之前一直滚动(不是动画)到顶部。这是第一个问题,因为它使用户难以跟踪它们的位置。

第二个问题是,当我退出新的视图控制器,返回到表视图控制器时,我无法从第一个单元格向下滚动。它反弹,我可以看到一些第二个细胞,但它不会降低。重新加载表会导致滚动再次起作用。

如何防止它滚动到顶部,为什么在滚动到顶部后限制滚动?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

@implementation PFQueryTableViewController
{
    UIViewController* _rootViewController;
    UINavigationController* _nav
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self objectAtIndexPath:indexPath]) { // Check our object exists
        MyNewViewController *card = [[MyNewViewController alloc] init];

        _nav = [[UINavigationController alloc] initWithRootViewController: card];
        [nav.navigationBar configureFlatNavigationBarWithColor: [UIColor pomegranateColor]];
        AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        _rootViewController = appDelegate.window.rootViewController;

        [UIView transitionFromView: _rootViewController
                            toView: _nav.view
                          duration: 0.5
                           options: UIViewAnimationOptionTransitionFlipFromRight |
         UIViewAnimationOptionAllowUserInteraction    |
         UIViewAnimationOptionBeginFromCurrentState
                        completion: ^(BOOL finished)
         {
             appDelegate.window.rootViewController = nav;
             [appDelegate.window makeKeyAndVisible];
         }];
    }
    else { // Otherwise, do what we would have done.
        [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    }
}

然后从navcontroller中调用下一个返回表格的方法:

- (void) returnToTheQueryTableViewController
{
    [UIView transitionFromView: _nav.view
                        toView: _rootViewController.view
                      duration: 0.5
                       options: UIViewAnimationOptionTransitionFlipFromRight |
     UIViewAnimationOptionAllowUserInteraction    |
     UIViewAnimationOptionBeginFromCurrentState
                    completion: ^(BOOL finished)
     {
         AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
         appDelegate.window.rootViewController = _rootViewController;
         [appDelegate.window makeKeyAndVisible];
     }];
}

希望这会对你有所帮助。