作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如何使用长按手势从 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!
}
最佳答案
嘿,下面的算法对你有用。
祝你好运:)
关于ios - 如何在要删除的单元格上使用 longtapGesture 从 Collection View 中删除 uicollectionviewcell? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48519594/
我是一名优秀的程序员,十分优秀!