1

Computer science student trying to learn Swift here. I am trying to create a very simple application for iPad which is essentially a food menu. The main page will just be table of restaurants and selecting a restaurant will bring the user to the restaurant's menu. The menu page has a Table View on the left side for navigating different food categories while on the right side is a Table View Controller of individual food items. Depending on which category the user selects, the table will refresh its data to show the respective food items.

EDIT 2: Essentially, it has a master-detail relationship like in a Split View Controller. However, I am not using the template because the the root view isn't going to be the Split View. I've managed to get the master and detail Tables to load the first page but I can't get the detail Table to refresh when I select a different category on the master Table. Here is some of my code:

MenuViewController is my Split View with the master Table View.

protocol CategorySelectionDelegate: class {
    func categorySelected(_ newCategory: Int)
}

class MenuViewController: UIViewController , UITableViewDelegate, UITableViewDataSource{

    weak var delegate: CategorySelectionDelegate?
    @IBOutlet weak var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
    }

    //... set up master Table View values

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        delegate?.categorySelected(indexPath.row)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destination = segue.destination as? ItemsTableViewController {
            destination.resto = resto
        }
    }
}

ItemsTableViewController is the detail Table View.

class ItemsTableViewController: UITableViewController {

    var resto: Restaurant!
    var categorySelected: Int = 0 {
        didSet {
            self.tableView.reloadData()
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    //...set up detail Table View cells

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
        cell.textLabel?.text = resto.menuItems[categorySelected][indexPath.row].name
        return cell
    }
}

extension ItemsTableViewController: CategorySelectionDelegate {
    func categorySelected(_ newCategory: Int) {
        categorySelected = newCategory
    }
}

FYI: MyRestaurantClass has an array cats: [String] for food categories and a double array menuItems[[MyFoodObject]] for food items separated into categories

I have looked in a lot of places for an answer such as:

https://useyourloaf.com/blog/container-view-controllers/

https://digitalleaves.com/blog/2017/01/towards-better-split-view-controllers-i-our-custom-split-controller/

https://www.raywenderlich.com/173753/uisplitviewcontroller-tutorial-getting-started-2

https://makeapppie.com/2015/03/31/swift-swift-split-view-controllers-round-two-part-2-master-to-detail/

Split View Controller not as a Root View Controller

I have also read Apple's documentation on View Controllers, Navigation Controllers, Delegation, etc

I think what I'm struggling with is how to set delegates properly. An example and explanation would be greatly appreciated. Thanks in advance.

kelelenceu
  • 81
  • 10
  • One TableView added as a childViewController. What do you mean with one childViewController per category again? If so can’t you do that with tableView sections, like `menus[section][row]`? I liked [Using child view controllers as plugins](https://medium.com/@johnsundell/using-child-view-controllers-as-plugins-in-swift-458e6b277b54) for learning child view controllers. – Fabian Jul 27 '18 at 00:37
  • In your case the Home View Controller could be a subclass of UISplitViewController. Just because the data in the detail views (secondary view controller) is going to be different it doesn't mean you need to build separate view controller for each of the details views as long as the layout is the same. Treat view controllers as a container for showing views. It could be configured / given different inputs so that different data is displayed. – user1046037 Jul 27 '18 at 01:09
  • IMHO, please take the time to learn the basics (language / iOS frameworks) before building the final app. It is time consuming to learn it but might serve you well in the long run – user1046037 Jul 27 '18 at 01:11
  • Thanks. @user1046037 do you have any recommendations for resources to learn the basics? – kelelenceu Jul 27 '18 at 16:36
  • To learn Swift: https://itunes.apple.com/sg/book/the-swift-programming-language-swift-4-2-beta/id1002622538?mt=11 To learn iOS app development: https://itunes.apple.com/sg/podcast/developing-ios-11-apps-with-swift/id1315130780?mt=2 – user1046037 Jul 27 '18 at 23:02
  • For iOS programming take the Stanford tutorials as a rough guideline, often you might need to pause the video and read a little Apple Documentation on the topic (Xcode / Web). The video is good but I found it a bit fast paced, so often paused and had to rewind and watch – user1046037 Jul 27 '18 at 23:04
  • @user1046037 thanks for the links. I'll read them as I continue with my project. Also i've updated my question according to some of the advice here. – kelelenceu Jul 28 '18 at 00:21
  • Go step by step debugging, 1. Check didSelect method is called 2. Check if delegate (`CategorySelectionDelegate`) is set and not nil, 3. See if `didSet` in `categorySelected` is called. 4. See if cellForRowAtIndexPath is called, 5. Check the contents of `resto.menuItems` (print the entire array may the contents are the same for 2 different indices.) – user1046037 Jul 28 '18 at 01:35
  • There are advantages of using a split view controller as a root controller and having navigation controllers for master and detail view controllers. Reason is split view on the iPad would adjust accordingly. Again best you learn the basics before picking from the middle, it might lead to lots of misconceptions and re-work – user1046037 Jul 28 '18 at 01:36
  • @user1046037 You're right. Turns out I didn't actually set my delegate. And thanks for the tips. I guess I'll go over basics more before I continue with my project. – kelelenceu Aug 02 '18 at 16:26

0 Answers0