2013-10-17 43 views
1

我有一個PFQueryTableViewController,它被設置爲實例化和呈現一個新的視圖控制器,在一個UINavigationViewController內,當一個行被選中。介紹另一個視圖控制器滾動UITableView頂部

- (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

嘗試是這樣的:

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

希望這有助於你。

+0

謝謝!我會給它一個鏡頭。只是爲了我的理解,是presentViewController做到這一點的錯誤方式? – tghw

+0

我不認爲你的解決方案是錯誤的。我認爲你應該嘗試我的解決方案:)。 – stosha

+0

或者你可以嘗試調用方法[tableView deselectRowAtIndexPath:indexPath animated:NO];在presentviewController之前保存當前indexPath並在返回後恢復。 – stosha

相關問題