5

What is the most common way to delete cells from a UICollectionView?

In a UITableView I use the editActionsForRowAt or editingStyle methods to delete rows, does UIControllerView has something similar or you need to implement your own deleting method?

What I have is a UICollectionView with a lot of photos where each cell/photo is attached to a segue which takes you to a larger version of the photo.

The easiest way I could be to do it in the didSelectItemAt method but in my case that is not an option since as soon as a photo is tapped it segues to the other viewController (larger image).

What would be the best way to add a deleting functionality in a situation like the one I'm describing above?

The following threads show how to delete using the didSelectItemAt.

How to add a delete button to Collection View Cell in Swift?
How to delete item from collection view?

CODE

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return fruits.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fruitCustomCell", for: indexPath) as! fruitCollectionViewCell

    cell.labelFruitName.text = fruits[indexPath.row].fruitName
    return cell
}

App Diagram

enter image description here

fs_tigre
  • 10,650
  • 13
  • 73
  • 146
  • 1
    I believe this has already been answered here: https://stackoverflow.com/questions/28748393/remove-uicollectionview-cells-on-edit-button – pesch Jul 04 '17 at 17:08
  • 2
    Try implement the way iOS apps are deleted from main menu ie. long clicking and starting edit mode and then on selection delete the cell. – Nikhil Manapure Jul 04 '17 at 17:11
  • Thanks you all for your suggestions, I will try to implement my own deleting method, may be by adding a delete-button to each image when an EDIT button is tapped and then assign a tag to each button to keep track of which images was tapped. Thanks – fs_tigre Jul 05 '17 at 12:27

0 Answers0