简体   繁体   English

Swfit 3:如何通过按钮操作获取单元格中的所有项目?

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

I have a button in my cell and I want to get all of the items in the cell with this button. 我的单元格中有一个按钮,我想使用此按钮获取单元格中的所有项目。

I tried this code, but it doesn't work. 我尝试了这段代码,但是没有用。 I hope you can get the idea that I want to do. 我希望您能得到我想要做的想法。

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

If your button is in the cell itself, then things are really easy. 如果您的按钮在单元格本身中,那么事情真的很简单。

The super view of your button would be the content view of the table view cell and the super view of that will be the UITableViewCell itself. 您的按钮的超视图将表视图单元格的内容和观点的,这将是超级视图UITableViewCell本身。

So your method should look like this: 因此,您的方法应如下所示:

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

tableView.dequeueReusableCell will just create a new cell, so using it here will not do what you want. tableView.dequeueReusableCell只会创建一个新的单元格,因此在此处使用它将无法完成您想要的操作。

you can create protocol 您可以创建协议

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

your UIViewController should 您的UIViewController应该

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

inside your UITableViewCell create 在您的UITableViewCell内部创建

weak var dataDelegate: DataPasser?

link your UIButton action to your cell and inside it do 将您的UIButton操作链接到您的单元格并在其中执行

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

and inside cellForRow of your UIViewController do 并在UIViewController cellForRow内部执行

cell.dataDelegate = self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM