簡體   English   中英

Swfit 3:如何通過按鈕操作獲取單元格中的所有項目?

[英]Swfit 3 : How can I get all items in the cell with button action?

我的單元格中有一個按鈕,我想使用此按鈕獲取單元格中的所有項目。

我嘗試了這段代碼,但是沒有用。 我希望您能得到我想要做的想法。

@IBAction func addToCardAction(_ sender: UIButton) {
    let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: menuTableView.indexPathForSelectedRow) as! SiparisVerTableViewCell
    print(cell.countLabel)

如果您的按鈕在單元格本身中,那么事情真的很簡單。

您的按鈕的超視圖將表視圖單元格的內容和觀點的,這將是超級視圖UITableViewCell本身。

因此,您的方法應如下所示:

@IBAction func addToCardAction(_ sender: UIButton) {
    let cell = sender.superview?.superview as! SiparisVerTableViewCell
    print(cell.countLabel)
}

tableView.dequeueReusableCell只會創建一個新的單元格,因此在此處使用它將無法完成您想要的操作。

您可以創建協議

protocol DataPasser: NSObjectProtocol {
    func passData(...any parameters)
}

您的UIViewController應該

extension MyViewController: DataPasser {
    func passData(...any parameters) {
        //manipulate it
    }
}

在您的UITableViewCell內部創建

weak var dataDelegate: DataPasser?

將您的UIButton操作鏈接到您的單元格並在其中執行

self.dataDelegate?.passData(...any parameters)

並在UIViewController cellForRow內部執行

cell.dataDelegate = self

暫無
暫無

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

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