长按手势操作表删除单元格
Posted
技术标签:
【中文标题】长按手势操作表删除单元格【英文标题】:Long press gesture action sheet delete cell 【发布时间】:2018-03-13 19:44:36 【问题描述】:我试图在集合视图中对单元格上的长按手势进行编码,但我没有找到类似的...我想我会在“didSelectItemAt”中编码一些东西。 现在只有当我点击第一个单元格时才有效...... 然后我在网上找到了基于 swift 3 的不同解决方案。
请大家帮帮我好吗?非常感谢!
The image show when tap long on the first cell the action sheet. But don't work when I tap to other cell...
import UIKit
class RecipeCollViewController: UICollectionViewController, UITextFieldDelegate
struct Storyboard
static let leftAndRightPaddings: CGFloat = 2.0
static let numberOfItemsPerRow: CGFloat = 2.0
override func viewDidLoad()
super.viewDidLoad()
RecipeDataManager.shared.recipeController = self
title = loc("TITLE_RECIPECOLL")
navigationController?.navigationBar.prefersLargeTitles = true
let collectionViewWidth = collectionView?.frame.width
let itemWidth = (collectionViewWidth! - Storyboard.leftAndRightPaddings) / Storyboard.numberOfItemsPerRow
let layout = collectionViewLayout as! UICollectionViewFlowLayout
layout.itemSize = CGSize(width: itemWidth, height: 250)
override func numberOfSections(in collectionView: UICollectionView) -> Int
return 1
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return RecipeDataManager.shared.recipes.count
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RecipeCell", for: indexPath) as! RecipeViewCell
let recipe = RecipeDataManager.shared.recipes[indexPath.item]
cell.labelNameRecipe.text = recipe.titleRecipe
cell.imageViewRecipe.image = recipe.imageRecipe
cell.labelPrepareTime.text = String(recipe.recipeTimeInt)
cell.labelPeopleFor.text = recipe.peopleRecipe
return cell
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
// MARK: - NAVIGAZIONE
// Metodo che scatta quando l'utente tocca una delle celle della collectionView e apre il dettaglio
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if segue.identifier == "RecipeDetail"
if let indexPath = self.collectionView!.indexPathsForSelectedItems?.first
let recipeDetailVC = segue.destination as! DetailRecipeViewController
recipeDetailVC.recipe = RecipeDataManager.shared.recipes[indexPath.item]
// MARK: - UILongPressGestureRecognizer function for the cell recipe
@IBAction func popUpActionCell(longPressGesture : UILongPressGestureRecognizer)
let alertActionCell = UIAlertController(title: "Action Recipe Cell", message: "Choose an action for the selected recipe", preferredStyle: .actionSheet)
// Configure Remove Item Action
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: action in
// Delete selected Cell
let deleteRecipe: [RecipeDataManager] = []
if let indexPath = self.collectionView?.indexPathsForSelectedItems?.first
RecipeDataManager.shared.recipes.remove(at: indexPath.item)
RecipeDataManager.shared.salva()
self.collectionView?.deleteItems(at: [indexPath])
print("Cell Removed")
)
// Configure Cancel Action Sheet
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: acion in
print("Cancel actionsheet")
)
alertActionCell.addAction(deleteAction)
alertActionCell.addAction(cancelAction)
self.present(alertActionCell, animated: true, completion: nil)
self.collectionView!.reloadData()
【问题讨论】:
【参考方案1】:很高兴,我找到了我的问题的最终解决方案。 为其他需要帮助的新开发人员发布代码!
// MARK: - Long Press Gesture Action Sheet
@IBAction func popUpActionCell(longPressGesture : UILongPressGestureRecognizer)
// Delete selected Cell
let point = longPressGesture.location(in: self.collectionView)
let indexPath = self.collectionView?.indexPathForItem(at: point)
// let cell = self.collectionView?.cellForItem(at: indexPath!)
if indexPath != nil
let alertActionCell = UIAlertController(title: "Action Recipe Cell", message: "Choose an action for the selected recipe", preferredStyle: .actionSheet)
// Configure Remove Item Action
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: action in
RecipeDataManager.shared.recipes.remove(at: indexPath!.row)
print("Cell Removed")
self.collectionView!.reloadData()
)
// Configure Cancel Action Sheet
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: acion in
print("Cancel actionsheet")
)
alertActionCell.addAction(deleteAction)
alertActionCell.addAction(cancelAction)
self.present(alertActionCell, animated: true, completion: nil)
【讨论】:
以上是关于长按手势操作表删除单元格的主要内容,如果未能解决你的问题,请参考以下文章