gpt4 book ai didi

ios - 如何与自定义 UICollectionViewCell 单元格交互

转载 作者:行者123 更新时间:2023-11-29 06:01:42 26 4
gpt4 key购买 nike

我有一个 UICollectionViewController,它垂直滚动(像表格 View 一样)。我创建了一个自定义 UICollectionViewCell。自定义单元格内部有复选标记。当用户单击复选标记时,我需要单击某种事件。

我尝试过的,是压倒一切的:

 override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("clicked")
}

但只有当用户单击单元格时才会执行。但是,正如我上面所说,单元格包含复选标记...我需要找出每个复选标记何时被单击。

这可能吗?

最佳答案

您可以将 IBAction 添加到 UICollectionViewCell 类并处理来自该 IBAction 的点击,如果您需要它来更改父 View Controller 上的某些内容,您有几个选项,您可以使用将 Controller 委托(delegate)或传递给单元。

在父 View Controller 上:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CustomCollectionViewCell
cell.controller = self
return cell
}

在 UICollectionViewCell 类上:

var controller: ParentViewController? 

@IBAction func checkmarkPressed(sender: UIButton) {

print("checkmarkPressed")

controller.someFunction()

}

关于ios - 如何与自定义 UICollectionViewCell 单元格交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54607964/

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