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://www.raywenderlich.com/173753/uisplitviewcontroller-tutorial-getting-started-2
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.