1

Using Xcode 4.6.1, iOS SDK 6.1, creating a new Master-Detail iOS application (with ARC, no storyboards) and in the DetailViewController I make configureView as:

- (void)configureView
{
    UITableView *lTableView = [[UITableView alloc] initWithFrame: self.view.frame];
    lTableView.scrollsToTop = YES;  // just to emphasise, it is the default anyway
    lTableView.dataSource = self;
    [self.view addSubview: lTableView];
}

Then I make sure there is enough data in the UITableView by returning 100 dummy UITableViewCells, it seems a tap on the status bar does not scroll the table view to the top.

What is the obvious thing I am missing here?

Kristof Van Landschoot
  • 1,427
  • 1
  • 12
  • 30

1 Answers1

1

Scrolling to the top of the view won't work if any other UIScrollView instance or subclass instance in the same window also has scrollsToTop set to YES because iOS doesn't know how to choose which one should scroll. In your case, configureView is actually called twice:

  • In viewDidLoad when the detail controller is loaded
  • In setDetailItem: when the master controller pushes to the detail controller

Because you're adding a UITableView as a subview in configureView, you end up with two table views, both with scrollsToTop set to YES. To fix the issue, create the table view in viewDidLoad and only use configureView to modify the base state as required for a given detail item.

- (void)viewDidLoad {
    [super viewDidLoad];

    UITableView *lTableView = [[UITableView alloc] initWithFrame: self.view.frame];
    lTableView.scrollsToTop = YES;
    lTableView.dataSource = self;
    [self.view addSubview: lTableView];

    [self configureView];
}
jszumski
  • 7,430
  • 11
  • 40
  • 53
  • There is a follow-up question here: http://stackoverflow.com/questions/15974893/scrollstotop-not-working-with-uiviewcontroller-containment which is closer to the actual problem I'm after, and on which I'm a bit stuck as well. Any help much appreciated. – Kristof Van Landschoot Apr 12 '13 at 15:11