Here is my current setup:
MainMenuViewController
-> SubMenuViewController
-> UserInputViewController
-> ResultViewController
All of my view controllers contains a tableView
.
When a user taps on a cell in MainMenuViewControlle
, it will segue to SubMenuViewController
. All fine and dandy.
Here is where it gets tricky, in SubMenuViewController
, there are cells that needs to instantiate another SubMenuViewController
because the sub menu options can be several levels deep.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let selectedNode = node?.childenNode[indexPath.row] else {
return
}
if selectedNode.isLeaveNode() {
performSegue(withIdentifier: "userInput", sender: self)
} else {
let subMenuViewController = SubMenuViewController(node: selectedNode)
self.navigationController?.pushViewController(subMenuViewController, animated: true)
}
When there are no child nodes, then it will segue to UserInputViewController
, but when there are more options, it needs to instantiate another SubMenuViewController
and the tableView
will populate itself based on cell the user had tapped previously until selectedNode.isLeaveNode()
is true (which means there won't be any child nodes).
This problem is when this code runs :
let subMenuViewController = SubMenuViewController(node: selectedNode)
self.navigationController?.pushViewController(subMenuViewController, animated: true)
it give me the following error:
fatal error: unexpectedly found nil while unwrapping an Optional value
From where I registered my cells which is here:
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SubMenuTableViewCell", bundle: bundle)
tableView.register(nib, forCellReuseIdentifier: "SubMenuCell")
All my tableView cells are instantiated using a xib file, and I have registered my cells in viewDidLoad()
Can anybody see the problem?
UPDATE
Here is the rest of my code:
UIViewController
class SubMenuViewController: UIViewController {
var node: Node?
init(node: Node) {
self.node = node
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = false
self.navigationItem.title = node?.value.rawValue
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "SubMenuTableViewCell", bundle: bundle)
tableView.register(nib, forCellReuseIdentifier: "SubMenuCell")
}
}
UITableViewDataSource
extension SubMenuViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return node!.childCount
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("called")
let cell = tableView.dequeueReusableCell(withIdentifier: "SubMenuCell", for: indexPath) as! SubMenuTableViewCell
let desciptionModule = node?.childenNode[indexPath.row].value
let description = Modules.description(module: desciptionModule!)
cell.title.text = description.main
cell.subtitle.text = description.sub
return cell
}
}
UITableViewDelegate
extension SubMenuViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 68
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let selectedNode = node?.childenNode[indexPath.row] else {
return
}
if selectedNode.isLeaveNode() {
performSegue(withIdentifier: "userInput", sender: self)
} else {
let subMenuViewController = SubMenuViewController(node: selectedNode)
self.navigationController?.pushViewController(subMenuViewController, animated: true)
}
}
}