UICollectionViewCell 正在访问它的 parent UICollectionViewController

UICollectionViewCell gaining access to it's parent UICollectionViewController

我在使用 UICollectionViewController 时遇到问题,它将自定义 UICollectionViewCell 注册为 header。请看附图。

Storyboard Screenshot

在中间的图中,是一个 UICollectionViewController,红色方块突出显示了一个位于 UICollectionViewCell 中的 UIImageView,注册为 header。我正在尝试实现右图的结果,这样当按下 header 单元格的 UIImageView 时,带有信息列表的 UIView 从底部向上滑动。

到目前为止,这是我的代码结构:

1) 包含信息列表的 UIView swift 文件。

2) 实例化 UIView 并将其添加到我的 UICollectionViewController 中的子视图。

由于UIImageView(信息圆圈图标)存在于UICollectionViewCell中,我将其设为userInteractionEnabled,并为其添加了一个TapGestureRecognizer。但是,由于我想从 UICollectionViewController 动画化 UIView,我不确定如何从我的 UICollectionViewCell header 文件访问 UICollectionViewController 中的 UIView 属性。

我知道这听起来很混乱,但如果有人能指出我正确的方向,或者可能建议 re-structuring 我的代码,我将不胜感激。如果您想了解更多信息,也请告诉我。非常感谢您的帮助。

编辑更新:非常感谢约翰的详细解释。乍一看,我认为该协议将简单地应用于 entire UICollectionViewCell,但掩盖了它是 delegate?.clicked() within 这一事实限制范围的手势识别器。约翰,非常感谢您的帮助。希望这可以帮助其他人解决类似问题。

为了到达那里,您应该在 UICollectionViewCell 上实现一个协议,您将在其中告诉父单元格已被点击以及它应该采取什么操作,如下所示:

protocol MyCellDelegate: class {
    func clicked()
    func other()
}

class MyCell: UIColletionViewCell  {
    weak var delegate : MyCellDelegate?

    //call delegate?.clicked() where you have the gesture recogniser 

}

然后在 cellForItemAtIndexPath 或任何使用 MyCell 的地方写入以下内容

 cell.delegate = self

然后为您的 class 实现扩展:

 extension MyCollectionView: MyCellDelegate {
     func clicked() {
     //present the view here (the slide up)
     }
  }