swift - 在委托中仅传递索引而不是 UICollectionViewCell
问题描述
我有自定义CollectionViewCell
,当用户单击位于collectionviewcell
thendelegate
方法中的按钮时,将被调用。它有效,但我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) -> ()
解决方案
推荐阅读
- c++ - 如果我只有这些参数的向量,我怎么能给出 n 个函数的参数?
- paypal - 贝宝订阅工作流程
- docker - Google 的 Container OS 可以与 Compute Engine 上的 gRPC 一起使用吗?
- aws-lambda - 我可以使用 AWS Lambda 直接从 SQS 消息中访问 API 端点吗?
- sql - SQL Server 中表之间的参照完整性
- python - 如何更改类中的变量并能够将其导入另一个文件?
- jquery - 确认闪烁
- python - 使用 TensorFlow 和 tf.data.Dataset 从文件夹中采样一系列图像
- php - PHP在解析JSON数据后将对象与数组混淆
- python-3.x - Keras、Tensorflow 在模型构建时保留所有 GPU 内存