Khant Thu Linn Khant Thu Linn - 4 years ago 156
iOS Question

iOS UITableview Scrolling stop half way

When user tap on tabbar button, I need to scroll my tableview to top and I set up like this.

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

Problem is that scrolling stop half way and it doesn't scroll to top. I need to tap like 2 - 3 times to scroll to top. Is there anything that I need to check? In my other projects, it is okay though. For this one, I use self-sizing cell.

Answer Source

You can use below code if you've at least one row

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tblListing scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download