简体   繁体   中英

scrollsToTop not working for simple Master-Detail iOS Application

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?

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

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