2017-08-24 72 views
0

我無法觸發某個操作,因爲我單擊了表格單元格。按下單元格後,按下單元格的背景僅爲灰色。但是這個行爲沒有執行。請幫忙。Uable在swift中觸發按下表格單元的操作

import UIKit 

class ViewController: UIViewController, UITableViewDataSource , UITableViewDelegate 
{ 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    var loki_images:[UIImage] = [UIImage(named: "images.png")!, 
           UIImage(named: "images (1).png")!, 
           UIImage(named: "images (2).png")!] 

    var persons = [ "Lokesh Ahuja" , "Raghav Mittal" , "Tul-Tul" ] 

    var identities = ["A","B","C"] 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int 
    { 
     return 1 
    } 

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") 

     let Name = persons[indexPath.item] 

     cell!.textLabel!.text = Name 

     let image = loki_images[indexPath.row] 

     cell!.imageView?.image = image 

     return cell! 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     let id = identities[indexPath.item] 
     let viewController = storyboard?.instantiateViewControllerWithIdentifier(id) 
     self.navigationController?.pushViewController(viewController!, animated: true) 
    } 
} 
+0

你有沒有爲您的tableView設置委託('tableView.delegate = self')? –

+0

你可以提供你的故事板的截圖。 –

回答

2

是你的UIViewController是導航控制器

,如果沒有,那麼你應該

self.present(viewController, animated: true, completion: nil) 
0

更換

self.navigationController?.pushViewController(viewController!, animated: true) 

有你proparly連接表的的UITableViewDelegate與視圖控制器在故事板。覈實。如果你不連接它比selectselect不會被調用。

enter image description here

相關問題