2016-06-13 35 views
0

我創建了一個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

+0

我想,這將是更好地開始在這一點上塞格斯。看看http://www.codingexplorer.com/segue-swift-view-controllers/ –

回答

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) 
    } 
} 
0

您不需要使用此代碼行: _ = tableView.indexPathForSelectedRow! 如果讓_ = 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 
相關問題