Beginning Collection Views · Challenge: Deleting Cells | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/6308-beginning-collection-views/lessons/9

It seems no need to sort the selectedItems when delete the items.

I’m not sure I’m following you. Can you provide clarification to your question, concern, or comment?

I really like the step-by-step instructions for the challenges.

Awesome! I’m glad its been helpful!

Mmmm I don’t know if this is a bug but

When you make this:
1.- select items state
2.- Select items
3.- delete items
4.- select items
5.- come back normal state
6.- select items state
7.- you can see one item select

why start with one item selected?

That sounds like a bug - I’ll take a look and see what I can do. Thanks for letting us know!

1 Like

I’ve also run into this issue…and can’t wrap my head around on how to fix it. The bug I found is by going through the following steps:

  1. I would click on any of the collection view items to go to its detail view screen.
  2. Click the back button to go back to the main view screen.
  3. Click on the edit button to go into edit mode.
  4. Without clicking on any items (no items are check marked), I click on the trash button. It would then delete the item I had initially clicked on in step 1 at the time when it was not in editing mode.

I was playing around with the code and added the a little snippet that I picked up in the next video “Cleaning up the UI”. To avoid the bug that I encountered earlier, one could add the following code in the function setEditing:

collectionView.indexPathsForSelectedItems?.forEach {
            collectionView.deselectItem(at: $0, animated: false)
        }

So then what I have in my setEditing function is the following:

    override func setEditing(_ editing: Bool, animated: Bool) {
            super.setEditing(editing, animated: animated)
            setupRightBarButtonItem()
        
        collectionView.allowsMultipleSelection = editing
        
        collectionView.indexPathsForSelectedItems?.forEach {
            collectionView.deselectItem(at: $0, animated: false)
        }
        
        let indexPaths = collectionView.indexPathsForVisibleItems
        for indexPath in indexPaths {
            let cell = collectionView.cellForItem(at: indexPath) as! CollectionViewCell
            cell.isEditing = editing
        }
    }

Hope that helps clear things up for anyone that comes across the same problem that I did.

@knicks135 Thank you for sharing your solution - much appreciated! :]

@knicks135 Nice solution to clear the selections with each edit toggle. Thanks for sharing!

@bdmoakley it might be a good idea to not fast forward the parts where you’re writing code. I find myself pausing and reading the code all the time, instead of following along with you.

Okay, thanks for the feedback. I’ll pass it on to the editors. Cheers!