如何在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 } } 

试试这个而不是你的didSelectRow方法:

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

您用于didSelect方法的“方法签名”(函数名称)不正确。 您当前实现中列出的是“Swift 2版本”。 您可能会注意到参数标签被分开以使函数更易读的方式,如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