gpt4 book ai didi

ios - 如何在要删除的单元格上使用 longtapGesture 从 Collection View 中删除 uicollectionviewcell? swift

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:11 25 4
gpt4 key购买 nike

如何使用长按手势从 Collection View 中删除所选项目并显示重新加载的数据,因为我无法使用 indexpath.row 方法执行此操作。我正在使用 UIAlertAction 来实现此操作。但是不能在 longtapGesture 上点击单元格。

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let mainstoryboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let detailVC = mainstoryboard.instantiateViewController(withIdentifier: "detailVC") as! DetailVC
detailVC.image = Model.dataList[indexPath.item].image
self.navigationController?.pushViewController(detailVC, animated: true)

let tapGes = UILongPressGestureRecognizer(target: self, action: #selector(self.didlongPress(tapGes:)))
self.view.addGestureRecognizer(tapGes)
}

// func removeItem(index : Int){
// Model.dataList.remove(at: index)
//
// let indexPath = IndexPath(row: index, section: 0)
// collectionView.performBatchUpdates({
// self.collectionView.deleteItems(at: [indexPath])
// }) { (finished : Bool) in
// self.collectionView.reloadItems(at: self.collectionView.indexPathsForVisibleItems)
// }
//
// }
//


func didlongPress(tapGes : UITapGestureRecognizer){
let actionController = UIAlertController(title: "Delete", message: "Are You Sure ...?", preferredStyle: .actionSheet)

let action = UIAlertAction(title: "Delete Current record", style: .default) { (action) in
print("Deleted")

self.collectionView.reloadData()
}

actionController.addAction(action)
self.present(actionController, animated: true, completion: nil)

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? CollectionCell
let note = Model.dataList[indexPath.item]
cell?.imageView.image = note.image
cell?.titleLbl.text = note.title


return cell!
}

最佳答案

嘿,下面的算法对你有用。

  • 点击时只需从数组“数据列表”中删除该项目indexPath 通过调用“datalist.remove(at: indexPath.row)”。
  • 之后只需调用“self.collectionView.deleteItemsAtIndexPaths([indexPath])”。
  • 这样您就不必重新加载 collectionView。

祝你好运:)

关于ios - 如何在要删除的单元格上使用 longtapGesture 从 Collection View 中删除 uicollectionviewcell? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com