为 UITableView 的每一行推送不同的 UIView

Push different UIView for each row of UITableView

当我 select 一行时,我正在尝试推送一个新视图。

let identifiers:[String] = ["vc1", "vc2", "vc3", "vc4", "vc5"]

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let identifier = identifiers[indexPath.row]
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("\(identifier)") as! UINavigationController
    self.presentViewController(vc, animated: true, completion: nil)

}

使用此 tableView 调用的所有不同视图都有自己的 class 和 Storyboard ID。

目前我得到错误:

Could not cast value of type 'testApp.CalculatorViewController' to 'UINavigationController'

我认为您正在尝试将 UIViewController 转换为 UINavigationController。

请试试这个。这可能对你有帮助。

let identifiers:[String] = ["vc1", "vc2", "vc3", "vc4", "vc5"]

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let identifier = identifiers[indexPath.row]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("\(identifier)") as! UIViewController
self.navigationController?.pushViewController(vc, animated: true, completion: nil)

}