2017-02-14 71 views
1

我想處理單擊我的控制器中的每個單元格,但我的日誌不顯示任何內容!如何處理單擊UITableViewController中的單元格

class RightMenuController: UITableViewController { 


    let row_items = ["دسته بندی", "ثبت نام/ورود"] 

    override func viewDidLoad() { 
     navigationController?.navigationBar.isHidden = true 
     super.viewDidLoad() 

     self.tableView.delegate = self 
     self.tableView.dataSource = self 


     // Uncomment the following line to preserve selection between presentations 
     // self.clearsSelectionOnViewWillAppear = false 

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
     // self.navigationItem.rightBarButtonItem = self.editButtonItem() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    // MARK: - Table view data source 

    override func numberOfSections(in tableView: UITableView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     // #warning Incomplete implementation, return the number of rows 
     return row_items.count 
    } 


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) 

     cell.textLabel?.text = row_items[indexPath.row] 


     return cell 

    } 


    /* 
    // Override to support conditional editing of the table view. 
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the specified item to be editable. 
     return true 
    } 
    */ 

    /* 
    // Override to support editing the table view. 
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
     if editingStyle == .delete { 
      // Delete the row from the data source 
      tableView.deleteRows(at: [indexPath], with: .fade) 
     } else if editingStyle == .insert { 
      // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
     }  
    } 
    */ 

    /* 
    // Override to support rearranging the table view. 
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { 

    } 
    */ 

    /* 
    // Override to support conditional rearranging of the table view. 
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 
     // Return false if you do not want the item to be re-orderable. 
     return true 
    } 
    */ 

    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


     print(self.row_items[indexPath.row]) // not print anything 
    } 

} 

回答

2

試試這個,而不是你didSelectRow法:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print(self.row_items[indexPath.row]) 
} 
0

「方法簽名」(函數名)所使用的方法didSelect是不正確。你目前實現中列出的是「Swift 2 Version」。您可能會注意到自變量標籤被分開的方式,以使功能更具可讀性,如tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)

這種相同的重命名樣式應該應用於didSelectRowAtIndexPath。這裏是完整的代碼,你可以用它來在操場上測試。

import UIKit 

class RightMenuController: UITableViewController { 

    let row_items = ["دسته بندی", "ثبت نام/ورود"] 

    override func viewDidLoad() { 
     // We must register a cell manually since there is no storyboard 
     tableView.register(UITableViewCell.self, forCellReuseIdentifier: "LabelCell") 
     navigationController?.navigationBar.isHidden = true 
     super.viewDidLoad() 
     self.tableView.delegate = self 
     self.tableView.dataSource = self 
    } 

    // MARK: - Table view data source 
    override func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) 
     cell.textLabel?.text = row_items[indexPath.row] 
     return cell 
    } 

    // Old Swift 2 method signature 
    //func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     print(self.row_items[indexPath.row]) // not print anything 
    } 
} 

// Create the ViewController 
let right = RightMenuController(style: .plain) 

// Ask the Playground to show your ViewController in the Assistant (2 rings - Right side view) 
import PlaygroundSupport 
PlaygroundPage.current.liveView = right 
相關問題