【问题标题】:Swift 5 CollectionView get indexPath by longPress cellSwift 5 UICollectionView 通过长按单元格获取 indexPath
【发布时间】: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


【解决方案1】:

首先使用 gesture.location(in:) 获取印刷机的坐标 Ref: https://developer.apple.com/documentation/uikit/uigesturerecognizer/1624219-location

然后使用indexPathForItem(at:) 检索被触摸单元格的IndexPath。参考:https://developer.apple.com/documentation/uikit/uicollectionview/1618030-indexpathforitem

基于此,您可能不需要为每个单元格使用不同的手势识别器,您可以将其注册到集合视图一次。


George Heints 基于上述提供的解决方案:

@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)
        }
    }
}

我建议使用 State.recognized 而不是 State.began,您的里程可能会有所不同!

【讨论】:

  • 感谢您,我添加了完整的代码(已更新),您能否将其添加到您的帖子中以供需要的人使用?
【解决方案2】:
import UIKit

extension UIResponder {

    func next<T: UIResponder>(_ type: T.Type) -> T? {
        return next as? T ?? next?.next(type)
    }
}

extension UICollectionViewCell {

    var collectionView: UICollectionView? {
        return next(UICollectionView.self)
    }

    var indexPath: IndexPath? {
        return collectionView?.indexPath(for: self)
    }
}

通过此扩展的帮助,您可以从集合视图单元格文件中了解集合视图的 indexPath。并且可以简单的从data数组中通过indexPath找到照片的id并删除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    相关资源
    最近更新 更多