表格视图中的单元格无响应

时间:2019-04-26 13:52:25

标签: ios swift xcode

我正在使用待办事项列表应用程序,每当我在simulator上运行它并尝试打印array中的项目时,其他单元格项目都会被打印。

这是我的代码:

import UIKit

class TodoListViewController: UITableViewController {
    let itemArray = ["math","english"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    // DATASOURCE METHODS

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  itemArray.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
        cell.textLabel?.text = itemArray[indexPath.row]
        return cell
    }

    // DELEGATE METHODS

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        print(itemArray[indexPath.row])
    }


}

1 个答案:

答案 0 :(得分:2)

您需要didSelectRowAt而不是didDeselectRowAt,当您选择一行时,后者会被触发,以便您从上一个选定的行中获取打印内容

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(itemArray[indexPath.row])
}
相关问题