如何与自定义UICollectionViewCell单元进行交互

时间:2019-02-09 15:59:57

标签: ios swift uicollectionviewcell

我有一个UICollectionViewController,它可以垂直滚动(如tableview)。我创建了一个自定义UICollectionViewCell。在自定义单元格中,有复选标记。当用户单击对勾标记时,我需要某种事件来单击。

我尝试过的是最重要的:

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

,但仅在用户单击单元格时执行。但是,如上所述,该单元格包含复选标记...我需要找出何时单击每个单独的复选标记。

这可能吗?

1 个答案:

答案 0 :(得分:0)

您可以将IBAction添加到UICollectionViewCell类中,并处理该IBAction中的点击,如果您需要它来更改父视图控制器上的某些内容,则可以有两个选择,可以使用委托或传递单元的控制器。

在父视图控制器上:

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()

}
相关问题