【发布时间】:2018-08-03 01:56:35
【问题描述】:
如何在 swift 4 中向每个水平集合视图单元格添加一个按钮并将其链接到一个动作
【问题讨论】:
标签: xcode uicollectionview uibutton swift4 uicollectionviewcell
如何在 swift 4 中向每个水平集合视图单元格添加一个按钮并将其链接到一个动作
【问题讨论】:
标签: xcode uicollectionview uibutton swift4 uicollectionviewcell
喜欢这个连接按钮 IBAction 在你的 CollectionViewCell 类中。
class PhotosCollectionViewCell: UICollectionViewCell {
@IBAction func DeleteTapped(_ sender: Any) {
if let OnDeleteTapped = self.OnDeleteTapped {
OnDeleteTapped(self)
}
}
var OnDeleteTapped : ((PhotosCollectionViewCell) -> Void)? = nil
}
在 CollectionView Delegate 方法中使用它。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell
cell.OnDeleteTapped = { cell in
// Do Anything Here is Your Action Event
}
return cell
}
【讨论】: