完整的 In-Depth switch 语句故障
Complete In-Depth switch statement malfunction
我以前问过这个问题一次,但我觉得我还没有问得那么透彻。我试图以编程方式完成一个非常标准的向下钻取 table 视图层次结构,而不是使用 IB 来避免不必要的混乱,因为我有超过 40 个不同的视图要实现。我决定使用以下 switch 语句:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var VC: UITableViewController
switch indexPath.row {
case 0: VC = SecondTableViewController()
default: ()
}
navigationController?.pushViewController(VC, animated: true)
}
如您所见,它给了我未初始化的错误,所以我继续使我的变量成为可选变量以解决此问题并编译并运行:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var VC: UITableViewController?
switch indexPath.row {
case 0: VC = SecondTableViewController()
default: ()
}
navigationController?.pushViewController(VC!, animated: true)
}
然而,当我 select 指定的行(在调试器下 运行 之后的值为 0 时它是正确的)它崩溃并出现此错误:
似乎是什么问题?它是我的开关中的默认语句吗?还是我的 "pushViewController" 方法中的变量?我可以添加,当我将此方法中的参数从 "VC/VC!" 更改为 "UITableViewController()" 时,如下所示:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var VC: UITableViewController?
switch indexPath.row {
case 0: VC = SecondTableViewController()
default: ()
}
navigationController?.pushViewController(UITableViewController, animated: true)
}
它相应地运行和运行,但是当视图被推送时,它不是我在 switch 语句中指定的 TableViewController,而只是一个空白的 table 视图。我错过了什么?
这是我的 SecondTableViewController 的代码:
导入 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
}
}
问题出在您没有为单元格定义标识符的 SecondTableViewController 中。你应该这样做,
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
}
}
我以前问过这个问题一次,但我觉得我还没有问得那么透彻。我试图以编程方式完成一个非常标准的向下钻取 table 视图层次结构,而不是使用 IB 来避免不必要的混乱,因为我有超过 40 个不同的视图要实现。我决定使用以下 switch 语句:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var VC: UITableViewController
switch indexPath.row {
case 0: VC = SecondTableViewController()
default: ()
}
navigationController?.pushViewController(VC, animated: true)
}
如您所见,它给了我未初始化的错误,所以我继续使我的变量成为可选变量以解决此问题并编译并运行:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var VC: UITableViewController?
switch indexPath.row {
case 0: VC = SecondTableViewController()
default: ()
}
navigationController?.pushViewController(VC!, animated: true)
}
然而,当我 select 指定的行(在调试器下 运行 之后的值为 0 时它是正确的)它崩溃并出现此错误:
似乎是什么问题?它是我的开关中的默认语句吗?还是我的 "pushViewController" 方法中的变量?我可以添加,当我将此方法中的参数从 "VC/VC!" 更改为 "UITableViewController()" 时,如下所示:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var VC: UITableViewController?
switch indexPath.row {
case 0: VC = SecondTableViewController()
default: ()
}
navigationController?.pushViewController(UITableViewController, animated: true)
}
它相应地运行和运行,但是当视图被推送时,它不是我在 switch 语句中指定的 TableViewController,而只是一个空白的 table 视图。我错过了什么?
这是我的 SecondTableViewController 的代码:
导入 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
}
}
问题出在您没有为单元格定义标识符的 SecondTableViewController 中。你应该这样做,
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
}
}