IOS Line selection in UISplitViewController on first boot

I have a setup UISplitViewController

in a universal app. This mostly works fine, but I have one small problem.

iPhone - only works in Portrait and loads MasterVC

(tableview) iPad correctly - works in both portrait and landscape

with Landscape it is loaded with the presence of the screen MasterVC

and DetailVC

. That's good, and I run the following code in viewDidAppear

in MasterVC

to select the first row if rows exist:

private func selectRowOnFirstLoadIfiPad() {
    if self.firstLoad && UIDevice.currentDevice().userInterfaceIdiom == .Pad {
        if self.fetchedResultsController.sections?.count != nil {
            let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0);
            self.myTableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition:UITableViewScrollPosition.None)
            self.performSegueWithIdentifier("mySegue", sender: self)
        }
        self.firstLoad = !self.firstLoad
    }
}

      

My problem is the user launches the app from the iPad in Portrait.

With the UISplitViewController

iPad goes right up to DetailVC

, and viewDidAppear

in MasterVC

never start (until the user presses the "Back" button). Then it tableView

slides along the edge of the screen and viewDidAppear

finally runs, calling the user to the first line.

How can I avoid this and also load the first line right away if they start iPad in portrait?

thank

+3


source to share


1 answer


When you run your session, I am assuming that you are loading a detailed view based on the selected cell. If so, try sending NSIndexPath as the sender object performSegueWithIdentifier

in viewDidLoad

your main view controller. Then in the prepareForSegue () file check if the sender object is of type NSIndexPath as the segue in the storyboard will be a UITableViewCell class. Get a string from this NSIndexPath and load the content into your detail view.

Then in viewDidAppear

(still in the main view controller) select the start line of the row so that it is highlighted.



func viewDidLoad() {
  // ...
  var initialIndexPath = NSIndexPath(forRow: 0, inSection: 0)
  self.performSegueWithIdentifier("mySegue", sender: initialIndexPath)
}

func viewDidAppear(_ animated: Bool) {
  // ...
  var initialIndexPath = NSIndexPath(forRow: 0, inSection: 0)
  self.myTableView.selectRowAtIndexPath(initialIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)
}

func prepareForSegue(_ segue: UIStoryboardSegue, sender sender: AnyObject?) {
  // ...
  if (sender.isKindOfClass(UITableViewCell) {
    // Get row from selected row in tableview
  } else if (sender.isKindOfClass(NSIndexPath) {
    // Get row from sender, which is an NSIndexPath
  } else {
    // Handle a default case
  }
  // Prepare the detail view with the index...
}

      

+1


source







All Articles