Ios 如何在UICollectionViewCell中的UITableViewCell中检测按钮点击?

Ios 如何在UICollectionViewCell中的UITableViewCell中检测按钮点击?,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,我正在尝试检测UICollectionViewCell中的UITableViewCell中的按钮点击 UICollectionView的委托和数据源是我的ViewController。每个UICollectionViewCell中都有一个UITableView。而UITableViewCell的委托和数据源是UICollectionViewCell 我需要检测ViewController中的按钮点击 视图控制器 @IBOutlet collectionView: UICollectionView

我正在尝试检测
UICollectionViewCell
中的
UITableViewCell
中的按钮点击

UICollectionView
的委托和数据源是我的
ViewController
。每个
UICollectionViewCell
中都有一个
UITableView
。而
UITableViewCell
的委托和数据源是
UICollectionViewCell

我需要检测
ViewController
中的按钮点击

视图控制器

@IBOutlet collectionView: UICollectionView!

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
    collectionView.dataSource = self
}
UICollectionView

@IBOutlet tableView: UITableView!

override func awakeFromNib() {
    super.awakeFromNib()
    tableView.delegate = self
    tableView.dataSource = self
}
UITableViewCell

@IBAction func buttonTapped(_ sender: CustomButton) {
    // Detect Button Tap in ViewController
}

我建议您使用委托模式


所以,表视图单元格和集合视图单元格需要一个协议

protocol TableCellDelegate: class {
     func buttonPressed(_ sender: CustomButton)
}

protocol CollectionCellDelegate: class {
     func buttonInTableCellPressed(_ sender: CustomButton)
}
现在为单元格子类创建
delegate
变量

class TableCell: ... {
    weak var delegate: TableCellDelegate?
}

class CollectionCell: ... {
    weak var delegate: CollectionCellDelegate?
}
继续执行
TableCellDelegate
CollectionView
并在表格单元格委托的方法中调用单元格委托的方法

extension CollectionCell: TableCellDelegate {
    func buttonPressed(_ sender: CustomButton) {
        delegate?.buttonInTableCellPressed(sender)
    }
}
下一步将CollectionCellDelegate执行到视图控制器

extension ViewController: CollectionCellDelegate {
    func buttonInTableCellPressed(_ sender: CustomButton) {
        ... // this is called when button in table view cell is pressed
    }
}
现在,不要忘记在
ViewController
中设置
cellForItemAt
内收集单元格的委托,在
CollectionCell
中设置
cellForRowAt
内表格单元格的委托

class ViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        ...
        cell.delegate = self
        ...
    }
}

class CollectionCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        ...
        cell.delegate = self
        ...
    }
}
现在,最后,当按下按钮时,在表单元格内的委托上调用方法

@IBAction func buttonTapped(_ sender: CustomButton) {
    delegate?.buttonPressed(sender)
}

你可以为此编写自己的委托书

protocol ButtonDelegate: class {
    func buttonTapped(_ button: UIButton)
}
然后,在您的
ViewController
中实现它,并在点击按钮时执行任何您想执行的操作

UICollectionView
UITableViewCell
中,添加一个属性:

弱变量buttonelegate:buttonelegate?

然后,修改您的
UITableViewCell

@IBAction func buttonTapped(_ sender: CustomButton) {
    buttonDelegate?.buttonTapped(sender)
}
ViewController
中,必须在
UICollectionView
上设置委托:

collectionView.buttonDelegate = self
最后,在
UICollectionView
中的
cellForRowAt
方法中,将
UITableViewCell
buttonelegate
属性设置为
UICollectionView
buttonelegate
,这是您的
ViewController

cell.buttonDelegate = buttonDelegate

在集合视图单元格类中添加一些额外的codecall delegate和tableView的datasource方法如果它是单元格中唯一的按钮,您可以依靠table view delegate didSelect,或者您可以创建一个委托模式或使用闭包在点击时通知控制器。我会将单元格传递给委托方法,而不是按钮。这样,学员就可以轻松地确定与按钮对应的适当数据模型项。@Paulw11从他的问题中不太清楚他需要什么按钮。无论如何,如果他需要传递适当的数据模型项,他可以传递该项而不是按钮,对吗?嘿,@RobertDresler,谢谢你的回答!看起来这也是一个正确的答案,但我接受了另一个答案,因为这是第一个答案,也解决了我的问题。祝你今天愉快@Sercan但是,他的答案不正确,他没有告诉您需要设置集合视图单元格的委托,因此他的答案不是第一个正确答案;)@罗伯特德雷斯勒个人认为,我不会像单元格那样将视图直接绑定到模型对象,我会将单元格传递给委托方法,而不是按钮。通过这种方式,代理可以轻松确定与按钮对应的适当数据模型项。如果不设置集合视图单元格的代理,这将如何工作?嘿@fabianbraach,感谢您的回答!我希望找到一种直接的方法从viewcontroller检测单元格中的点击,但我想这就是它的工作原理。这就像将委托(?)移动到上层阶级,对吗?在这种情况下我应该一直这样做吗?