mercredi 24 juin 2015

How to have multiple table views in a Master-Detail application?

Scenario

I would like to mimic the stock Mail app in iOS. In the Mail app you can select an inbox account (1st UITableView), select an email from a list (2nd UITableView) and finally be presented with your selected email (Detail View Controller).

To do that, the template that best fits this layout is "Master-Detail". This template comes out of the box with a single UITableView. I would like, however, to add an extra UITableView in my app because one is not enough. How would I go about doing that?

My approach

I placed a new UITableView in my Storyboard and connected it with a UINavigationController using the "Relationship root view controller" segue. I then connected the 1st UITableView to the UINavigationController I just created using the "show" segue. Finally, I I subclassed a new UITableView class for the 2nd table view, I added cells and reusable identifiers).

What's not working

When I tap cells in the 1st UITableView, they just highlight. I cannot access the 2nd UITableView. Do I have to use the prepareForSegue method? Here's the code I used so far. SecondController is the 2nd UITableView.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = objects[indexPath.row] as! String
            if let navController = (segue.destinationViewController as? UINavigationController) {
                let controller = navController.topViewController as! SecondController
                controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem()
            }
        }
    }
}

How to have multiple table views in a Master-Detail application?

See more ..

Aucun commentaire:

Enregistrer un commentaire