Robert Tillman
Robert Tillman

Reputation: 1013

Complete In-Depth switch statement malfunction

I have asked this question once before however I feel like i haven't been as thorough as can be. I am attempting to complete a very standard drill down table view hierarchy programmatically rather than using the IB to avoid unnecessary scramble due to the fact i have well over 40 different views i want to implement. I have decided to use the following switch-statement:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var VC: UITableViewController
    switch indexPath.row {
    case 0: VC = SecondTableViewController()
    default: ()
    }
    navigationController?.pushViewController(VC, animated: true)
}

enter image description here

as you can see it gives me the non-initialized error, so i then proceed to make my variable an optional to fix this issue & it compiles and runs:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var VC: UITableViewController?
    switch indexPath.row {
    case 0: VC = SecondTableViewController()
    default: ()
    }
    navigationController?.pushViewController(VC!, animated: true)
}

enter image description here

however when i select the designated row (which is correct at the value of 0 after running under the debugger) it crashes with this error:

enter image description here

what seems to be the issue? is it the default statement within my switch? or is it the variable within my "pushViewController" method? Might i add, when i change the parameter within this method from "VC/VC!" to "UITableViewController()" like such:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var VC: UITableViewController?
    switch indexPath.row {
    case 0: VC = SecondTableViewController()
    default: ()
    }
    navigationController?.pushViewController(UITableViewController, animated: true)
}

enter image description here

it runs & functions accordingly, but when the view is pushed, it is not to the TableViewController i designated in my switch statement, rather then just a blank table view. what am i missing?

HERE IS THE CODE FOR MY SecondTableViewController :

import UIKit

class SecondTableViewController: UITableViewController {

var myVariable = ["LIST OF STRINGS IN AN ARRAY"]

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

 override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()

}



 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return myVariable.count
}


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

    var superVariable = myVariable [indexPath.row]

    cell.textLabel!.text = superVariable


    return cell

}

}

Upvotes: 0

Views: 224

Answers (1)

rdelmar
rdelmar

Reputation: 104082

The problem lies in SecondTableViewController where you didn't define an identifier for your cell. You should do something like this,

class SecondTableViewController: UITableViewController {

    let theData = ["one", "two", "three", "four"]

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    }


    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return theData.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
        cell.textLabel?.text = theData[indexPath.row]
        return cell
    }
}

Upvotes: 1

Related Questions