【发布时间】:2019-10-20 21:24:04
【问题描述】:
当我长按单元格时,我正在寻找获取 indexPath 或数据的方法。基本上我可以从 collectionView 中删除专辑,为此我需要获取 id。
我的 cellForItem
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AlbumCollectionViewCell", for: indexPath) as! AlbumCollectionViewCell
cell.data = albumsDataOrigin[indexPath.row]
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPressGetstureDetected))
cell.addGestureRecognizer(longPressGesture)
return cell
}
longPressGetstureDetected
@objc func longPressGetstureDetected(){
self.delegateAlbumView?.longPressGetstureDetected()
}
删除函数
func longPressGetstureDetected() {
showAlertWith(question: "You wanna to delete this album?", success: {
self.deleteAlbum() //Here i need to pass ID
}, failed: {
print("Delete cenceled")
})
}
对于寻找完整答案的人
@objc func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == UIGestureRecognizer.State.began {
let touchPoint = longPressGestureRecognizer.location(in: collectionView)
if let index = collectionView.indexPathForItem(at: touchPoint) {
self.delegateAlbumView?.longPressGetstureDetected(id: albumsDataOrigin[index.row].id ?? 0)
}
}
}
【问题讨论】:
-
UICollectionVIewCell文件中的手势功能好吗?
标签: swift uicollectionview long-press