2017-02-02 54 views
0

当用户点击tabbar按钮时,我需要滚动我的tableview到顶部,并且我设置为这样。iOS UITableview滚动中途停止

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    UIViewController *vc = ((UINavigationController *)viewController).viewControllers[0]; 

    static UIViewController *previousController = nil; 
    if (previousController == vc) { 
     // the same tab was tapped a second time 

     SEL selector = NSSelectorFromString(@"scrollToTop"); 
     ((void (*)(id, SEL))[vc methodForSelector:selector])(vc, selector); 

    } 
    previousController = vc; } 


- (void)scrollToTop { 
    [self.tblListing setContentOffset:CGPointZero animated:NO]; 
} 

问题是滚动停止一半,它不会滚动到顶部。我需要点击2 - 3次才能滚动到顶部。有什么我需要检查?在我的其他项目中,尽管如此。对于这个,我使用自我大小的细胞。

回答

1

,如果你已经至少有一行

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
[self.tblListing scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
+0

感谢您可以使用下面的代码。它正在处理你的建议。我仍然需要找出为什么我的内容偏移方法不起作用。 –

+0

可能是这个职位帮助你:http://stackoverflow.com/q/19243177/468724 –

+0

让我看看。 –