gpt4 book ai didi

ios - UICollectionViewCell 获得对其父 UICollectionViewController 的访问权限

转载 作者:搜寻专家 更新时间:2023-10-31 22:12:19 25 4
gpt4 key购买 nike

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

Storyboard Screenshot

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

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

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

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

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

我知道这听起来确实令人困惑,但如果有人能为我指出正确的方向,或者可能建议重新构建我的代码,我将不胜感激。如果您想了解更多信息,也请告诉我。非常感谢您的帮助。

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

最佳答案

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

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

然后为你的类实现扩展:

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

关于ios - UICollectionViewCell 获得对其父 UICollectionViewController 的访问权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41742849/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com