当用户点击tableView单元格时,如何移动到其他viewControllers

时间:2016-07-25 12:19:31

标签: ios swift uitableview

我有一个带有表格视图控制器的导航栏,可显示4个图像(4个单元格)。当用户点击单元格时,我需要显示4个viewControllers(每个图像显示一个特定的VC)??

任何想法都会非常感激

这是我的代码:

class MenuTable: UITableViewController {

    @IBOutlet weak var imgtable: UITableView!

    var menuImage: [UIImage] = [
        UIImage(named: "image1")!,
        UIImage(named: "image2")!,
        UIImage(named: "image3")!,
        UIImage(named: "image4")!
    ]

    override func viewDidLoad() {
        super.viewDidLoad()

        imgtable.dataSource = self
        imgtable.delegate = self
    }

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

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("menucell") as! Menucell
        cell!.imageview.image = menuImage[indexPath.row]

        return cell
    }

}

1 个答案:

答案 0 :(得分:0)

您需要实现didSelectRowAtIndexPath这样的UITableView委托方法

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var viewController: UIViewController = UIViewController()
    switch (indexPath.row) {
        case 0:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController1") as! viewController1
        case 1:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController2") as! viewController2
        case 2:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController3") as! viewController3
        case 3:
            viewController = self.storyboard?.instantiateViewControllerWithIdentifier("viewController4") as! viewController4
        default:
            print("default")
    }
    self.navigationController?.pushViewController(viewController, animated: true)
}