简体   繁体   中英

TTTableViewController scrollsToTop not working

I'm using a basic Three20 TTTableViewController subclass which employs its own datasource and model.

The problem is that I cannot seem to use the scrollsToTop property of the table. This is a standard property for the table, inherited from UIScrollView and very commonly used.

I have tried all of the following, in numerous different locations/methods within my class:

self.tableView.scrollsToTop = YES;
[self.tableView setScrollsToTop:YES]


I have also tried overriding the method

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    return YES; 
}

All without success.

NB. To be clear, I am referring to the standard gesture of tapping on the status bar to scroll a visible table view to the top (ie first row).

Any help much appreciated!

There is some confusion

According to Apple documentation of UIScrollView :

scrollsToTop

A Boolean value that controls whether the scroll-to-top gesture is effective

This is just to tell scrollview that taping status bar will make view scroll to top.

What you are looking for is this UITableView method :

scrollToRowAtIndexPath:atScrollPosition:animated:

Scrolls the receiver until a row identified by index path is at a particular location on the screen.

With three20

Since you are using three20 what you want to do can be easily done thanks to its UITableView+Additions.h > - (void)scrollToFirstRow:(BOOL)animated

[self.tableView scrollToFirstRow:YES];

Without three20

If not using three20 , here is how three20 does:

- (void)scrollToFirstRow:(BOOL)animated {
  if ([self numberOfSections] > 0 && [self numberOfRowsInSection:0] > 0) {
    NSIndexPath* indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop
          animated:NO];
  }
}

Another way to do this

I think that would also work (untested), using UIScrollView directly, (extracted from three20 too):

- (void)scrollToTop:(BOOL)animated {
  [self setContentOffset:CGPointMake(0,0) animated:animated];
}

or :

[self.tableView setContentOffset:CGPointMake(0,0) animated:YES];


Thanks to Vincent G for pointing me in the right direction. It appears that the issue is to do with what code is called in the class's init and viewDidLoad methods.

I discovered that under the following conditions, two tableviews are added as subviews of the view controller's view:

  1. Making reference to the property tableView in the viewDidLoad method, eg [[self tableView] setTableHeaderView:]

  2. Building the dataSource of the table within the init / initWithStyle method.

With two UIScrollViews present, the scroll-to-top action does not work properly, as Vincent pointed out. I will file these findings as a bug with the Three20 guys but by following these steps it can at least be avoided for now.

Edit: It seems that this maybe due to the viewDidLoad method being called BEFORE the init in some cases. If reference is made to the tableview in the viewDidLoad, before it has been created, I think the class is creating one. The init will then make another one.

try this:

self.tableview.scrollingEnabled=YES;
self.tableview.scrollsToTop=YES;

Also check that your delegate is returning YES in this method:

scrollViewWillScrollToTop:

As @imnk suggested, my thoughts as an answer, even though I don't have a real solution.

What I'm experiencing (and just verified to be sure) is, that once I change the frame property of the tableView, the touch on the statusBar doesn't work anymore...

According to the Apples documentation on UIScrollViewDelegate , every scrollView's delegate (regardless of its size) can return YES on - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView .

But I looked through three20 and changed it in my implemetations of delegates, but with no success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM