簡體   English   中英

表格視圖中的單元格無響應

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM