簡體   English   中英

DidselectRow委托方法無法快速運行

[英]DidselectRow delegate method not working in swift

我有一個警報,其中我使用表視圖顯示了一些數據。

用戶可以從表視圖數據中選擇任何項目。

現在,當我嘗試在didSelectRow方法中選擇任何項目時,就沒有選擇任何項目。 我也使用過斷點,但是它也沒有捕獲斷點。

我已經給它的委托人和數據源一切都很好,但是我很困惑為什么它不起作用?

我的代碼是這樣的:

extension YourOrdersViewController : UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return PreOrderService.instance.PreOrderModelInstance.count
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Hello")
        print(PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time)
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        if tableView == orderTableView{
            print("Dishes")
        }
        else{
            orderTableView.cellForRow(at: indexPath)?.accessoryType = .none
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = orderTableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath) as!
    PreOrderTableViewCell

        cell.titleLbl.text = PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time
        print(cell.titleLbl.text!)
        cell.selectionStyle = .none
        cell.selectionStyle = UITableViewCellSelectionStyle.none
        cell.accessoryType = cell.isSelected ? .checkmark : .none
        return cell
    }
}

View Controller如下所示, 在此處輸入圖像描述

檢查或您已設置此值: 在此處輸入圖片說明 如果未設置No Selection而不是Single Selection ,則將不執行func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)方法。

它在UIViewControlller上是否有任何手勢識別器。 如果是這樣,並且手勢識別器位於響應者鏈上的表格視圖之上,則只需觸摸一下,就不會留下任何表格視圖。

我認為您代碼中的整個問題是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    self.tableView.cellForRow(at: indexPath)?.accessoryType = .none

}

確保已設置委托和數據源。 如果要通過情節提要板創建視圖,請確保為委托/數據源設置iboutlet。

這兩行含義相同:因此刪除第二行。

cell.selectionStyle = .none
cell.selectionStyle = UITableViewCellSelectionStyle.none

在Main.storyboard中選擇您的表視圖,然后在“屬性檢查器”中將選擇更改為單個選擇

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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