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