2016-06-07 61 views
-2

我想從UITableView Cell,當點擊單元格,去在另一個ViewController迅速。我有一個名爲MainMenu的viewController,它是我的UITableView,我正嘗試點擊一個單元格,然後轉到我的其他ViewController。有人可以幫忙嗎?UITableView單元到一個新的ViewController

我的UITableView的代碼調用主菜單:

class MainMenu: UITableViewController { 

    // Set Tabs in Table View Controller 
    var tabs = [String]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Name of Tabs 
     tabs = ["Scanner","QR-Codes","Cargo","Matrix","Xbox","PS4","Nintendo","Sega","Dreamcast","Xbox360","GameCube","Wii","Challenger","Mustang","Macbook","Logitech"] 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return tabs.count 
    } 
    // Set Tabs in View 
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCellWithIdentifier("scannerCell", forIndexPath: indexPath) as UITableViewCell 
     cell.textLabel!.text = tabs[indexPath.row] 
     print(tabs) 
     // return 
     return cell 
    } 
} 
+1

這只是繪製屏幕的代碼。當你點擊一個單元格或移動到另一個viewController時,你無處可做。如果你問如何做到這一點......有(數百個)stackoverflow問題和數以千計的教程來展示如何做到這一點。在提出問題之前,請做一些調查並嘗試一下。 –

+0

對不起剛纔需要幫助@SimonMcLoughlin –

回答

1

你將不得不實施2項:

1)創建的TableView之間的SEGUE你想要去的ViewController。命名它獨特的東西。

2)在您的ViewController(即MainMenu)中創建一個prepareForSegue函數。像這樣:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "UNIQUE NAME FROM PART 1" 
    { 
     if let destinationVC = segue.destinationViewController as? OtherViewController { 
      // pass an object here if necessary using destinationVC and dot syntax. 
     } 
    } 
} 
0

您需要實現tableViewtableView(_:didSelectRowAtIndexPath:)委託方法,並在它應該出現新的視圖控制器。

您可以使用UIViewControllerpresentViewController(_:animated:)方法以模態形式呈現它。

或者,如果您正在使用故事板,則從原型單元格創建一個Segue到要呈現的視圖控制器。

0

覆蓋didSelectRowAtIndexPath方法,搶在indexPath的數據源的項目,然後顯示您的視圖控制器

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    // Sudo code but you should get the idea 
    let vc: UIViewController = UIViewController() 
    // push it 
    navigationController?.pushViewController(vc, animated: true) 
    // or present it 
    presentViewController(vc, animated: true, completion: nil) 
} 
+0

謝謝!我很感激! –

相關問題