如何处理 UITableViewController 中的点击单元格

S.M*_*ian 3 uitableview ios swift

我想处理单击控制器中的每个单元格,但我的日志没有显示任何内容!

\n\n
class RightMenuController: UITableViewController {\n\n\n    let row_items = ["\xd8\xaf\xd8\xb3\xd8\xaa\xd9\x87 \xd8\xa8\xd9\x86\xd8\xaf\xdb\x8c", "\xd8\xab\xd8\xa8\xd8\xaa \xd9\x86\xd8\xa7\xd9\x85/\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf"]\n\n    override func viewDidLoad() {\n        navigationController?.navigationBar.isHidden = true\n        super.viewDidLoad()\n\n        self.tableView.delegate = self\n        self.tableView.dataSource = self\n\n\n        // Uncomment the following line to preserve selection between presentations\n        // self.clearsSelectionOnViewWillAppear = false\n\n        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.\n        // self.navigationItem.rightBarButtonItem = self.editButtonItem()\n    }\n\n    override func didReceiveMemoryWarning() {\n        super.didReceiveMemoryWarning()\n        // Dispose of any resources that can be recreated.\n    }\n\n    // MARK: - Table view data source\n\n    override func numberOfSections(in tableView: UITableView) -> Int {\n        // #warning Incomplete implementation, return the number of sections\n        return 1\n    }\n\n    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {\n        // #warning Incomplete implementation, return the number of rows\n        return row_items.count\n    }\n\n\n    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {\n\n\n        let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)\n\n        cell.textLabel?.text = row_items[indexPath.row]\n\n\n        return cell\n\n    }\n\n\n    /*\n    // Override to support conditional editing of the table view.\n    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {\n        // Return false if you do not want the specified item to be editable.\n        return true\n    }\n    */\n\n    /*\n    // Override to support editing the table view.\n    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {\n        if editingStyle == .delete {\n            // Delete the row from the data source\n            tableView.deleteRows(at: [indexPath], with: .fade)\n        } else if editingStyle == .insert {\n            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view\n        }    \n    }\n    */\n\n    /*\n    // Override to support rearranging the table view.\n    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {\n\n    }\n    */\n\n    /*\n    // Override to support conditional rearranging of the table view.\n    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {\n        // Return false if you do not want the item to be re-orderable.\n        return true\n    }\n    */\n\n    /*\n    // MARK: - Navigation\n\n    // In a storyboard-based application, you will often want to do a little preparation before navigation\n    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {\n        // Get the new view controller using segue.destinationViewController.\n        // Pass the selected object to the new view controller.\n    }\n    */\n\n    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {\n\n\n        print(self.row_items[indexPath.row]) // not print anything\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Nat*_*tes 5

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

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