【问题标题】:Presenting Another View Controller Scrolls UITableView to Top呈现另一个视图控制器将 UITableView 滚动到顶部
【发布时间】:2013-10-17 00:59:57
【问题描述】:

我有一个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];
    }
}

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

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

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案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 调用 next 方法返回表:

    - (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];
         }];
    }
    

    希望对你有所帮助。

    【讨论】:

    • 谢谢!我会试一试。只是为了我的理解,presentViewController 是错误的方法吗?
    • 我认为您的解决方案没有错。我认为您应该尝试我的解决方案:)。
    • 或者你可以尝试调用方法 [tableView deselectRowAtIndexPath:indexPath animated:NO];在 presentviewController 之前保存当前 indexPath 并在返回后恢复。
    • presentViewController 会导致前一个视图控制器被释放。这可能与您看到的问题有关。
    • @MikeHay 谢谢!这是有道理的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 2017-02-03
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多