簡體   English   中英

如何與自定義UICollectionViewCell單元進行交互

[英]how to interact with custom UICollectionViewCell cell

我有一個UICollectionViewController,它可以垂直滾動(如tableview)。 我創建了一個自定義UICollectionViewCell。 在自定義單元格中,有復選標記。 當用戶單擊對勾標記時,我需要某種事件來單擊。

我嘗試過的是:

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

但這僅在用戶點擊單元格時執行。 但是,如上所述,該單元格包含復選標記...我需要找出何時單擊每個單獨的復選標記。

這可能嗎?

您可以將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()

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM