点击来自tableview的单元格时没有任何反应

时间:2016-06-13 18:01:45

标签: swift uitableview tableviewcell

我创建了一个tableview菜单,当你点击一个单元格来显示特定的故事板时我想要这个。菜单格式为4个单元格(因此4个不同的故事板)

这是我的代码:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        _ = tableView.indexPathForSelectedRow!
        if let _ = tableView.cellForRowAtIndexPath(indexPath){
        if indexPath.row == 0{
            print("User choose to buy ticket")
            self.storyboard?.instantiateViewControllerWithIdentifier("SearchPage")
        }else if indexPath.row == 1{
            print("User choose to check the train status")
            self.storyboard?.instantiateViewControllerWithIdentifier("TrainStatusPage")
        }else if indexPath.row == 2{
            print("User choose to see the upcoming trips")
            self.storyboard?.instantiateViewControllerWithIdentifier("TripsPage")
        }else if indexPath.row == 3{
            print("User wants to know mor about CFR's facility")
            self.storyboard?.instantiateViewControllerWithIdentifier("PassengersPage")
        }
        }
    }

当我按下细胞(无关紧要)时,细胞会变灰并且什么也没发生。

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您不需要使用以下代码行:  _ = tableView.indexPathForSelectedRow!         if let _ = tableView.cellForRowAtIndexPath(indexPath){

只需检查方法didSelectRowAtIndexPath收到了什么indexPath.row。

所以正确的方式将是这样的:

if indexPath.row == 0{
            print("User choose to buy ticket")
            self.storyboard?.instantiateViewControllerWithIdentifier("SearchPage")
        }else if indexPath.row == 1{
            print("User choose to check the train status")
            self.storyboard?.instantiateViewControllerWithIdentifier("TrainStatusPage")
        }else if indexPath.row == 2{
            print("User choose to see the upcoming trips")
            self.storyboard?.instantiateViewControllerWithIdentifier("TripsPage")
        }else if indexPath.row == 3{
            print("User wants to know mor about CFR's facility")
            self.storyboard?.instantiateViewControllerWithIdentifier("PassengersPage")
        }

并且您可以添加此行以从单元格中删除选择状态:

tableView(tableView: UITableView, didSelectRowAtIndexPath

答案 1 :(得分:0)

你可能打算导航到下一个viewController?然后你需要这样说,初始化一个实例只是工作的一半(如果这确实是你想要做的)。所以某些如下所示:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var viewController: UIViewController?
    if indexPath.row == 0 {
        viewController = storyboard?.instantiateViewControllerWithIdentifier("SearchPage")
    }else if indexPath.row == 1 {
        viewController = storyboard?.instantiateViewControllerWithIdentifier("TrainStatusPage")
    }else if indexPath.row == 2 {
        viewController = storyboard?.instantiateViewControllerWithIdentifier("TripsPage")
    }else if indexPath.row == 3 {
        viewController = storyboard?.instantiateViewControllerWithIdentifier("PassengersPage")
    }

    if let destination = viewController {
        navigationController?.pushViewController(destination, animated: true)
    }
}