iosswiftuitableviewgrouped-table

Swift grouped UITableView best practices


I'm setting up the settings page of my first Swift app and can't seem to find a clean way to set up the master-detail relationship between the settings view and a child table view of options.

Interface Builder setup

My settings page is a UITableViewController with a custom layout of grouped cells. I also have a detail UITableViewController that I want to use to present options for various settings. My initial thinking led me to think that I can use a single view controller for this and just change the cells based on the segue. My largest problem is loading all the data into the cells since the grouped tables don't have prototype cells that I can call in

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) {}

Is there a commonly accepted way to set up these sort of settings table, that have a defined number of rows? I could make a viewcontroller for each setting, but that feels wasteful.


Solution

  • Even though it feels wasteful, it's more intuitive and less convoluted to have (segues to) different view controllers.

    Your detail view controller has to

    So, there are three points where you'd end up with large blocks of conditional code to determine which group of options you're dealing with, while displaying, unwinding, or preserving/restoring details.

    If you added more options, a monolithic approach would only continue to grow more and more complex.

    A monolithic approach is also the least flexible, if you ever needed to support a second (non-check mark) type of selection in the future.

    Separating responsibilities into different (subclassed) detail view controllers means each controller can clearly and simply handle its own details. Your code will be easier to understand, maintain, and update down the road.