首页 > 解决方案 > 在委托中仅传递索引而不是 UICollectionViewCell

问题描述

我有自定义CollectionViewCell,当用户单击位于collectionviewcellthendelegate方法中的按钮时,将被调用。它有效,但我UICollectionViewCell作为参数传递。但是,它效率不高。

我想只传递选定的 customviewcellindex而不是整个 Cell 对象。你将如何做到这一点?

protocol CustomCollectionViewCellDelegate: AnyObject {
    func greenButtonTapped(cell: UICollectionViewCell)
}

class CustomCollectionViewCell : UICollectionViewCell {
    
    weak var delegate: CustomCollectionViewCellDelegate?

    @IBAction func greenButtonTapped(_ sender: Any) {
        delegate?.greenButtonTapped(cell: self)
    }
}

或类似的东西closure

var onTappingGreenButton: (UIButton, IndexPath) -> ()

标签: swift

解决方案


推荐阅读