[英]Cells in table view not responding
我正在使用待辦事項列表應用程序,每當我在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])
}
}
您需要didSelectRowAt
而不是didDeselectRowAt
,當您選擇一行時會觸發后者,因此您可以從上一個選定的行中獲取打印內容
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(itemArray[indexPath.row])
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.