Challenge: Deleting Emoji | raywenderlich.com


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

Hi, this is a simple question but made me think. When I delete all the items of a section except one, this item is centered on the screen horizontally. Is this behavior normal? And is there a way to change it so it is aligned to the left?.
Screen Shot 2020-01-04 at 21.57.01

@optastudent Nice find! Iโ€™m tempted to say this is a bug. Iโ€™ll try and replicate this on my end and find a fix. Meanwhile I found this Stack Overflow thread that has a solution

@pasanpr, when we override setEditing func we switch on delete button and off add one. How are they toggled back again since we do not do it intentionaly in code?

These are the lines of code that toggle the buttons on and off:

deleteButton.isEnabled = isEditing
addButton.isEnabled = !isEditing

When we tap on the edit button, isEditing is true which enables the delete button by setting deleteButton.isEnabled to true and the hides the add button by flipping its enabled value to false using the NOT operator.

Similarly when we tap on the done button, isEditing is now false and toggles the respective values on each button.

1 Like

@pasanpr, does that happen because isEditing is a property of a class (not a struct)?

@pasanpr Can you please help with this when you get a chance? Thank you - much appreciated! :]

1 Like

It doesnโ€™t necessarily have to do with being a struct or a class. You could achieve it with both. When you tap on the edit button one of the actions it executes is to set the value on isEditing to true or false depending on the state. Since weโ€™re observing the value of isEditing to set our button properties, tapping the button triggers it.

In this particular case the view controller is a class but this is also possible with a struct (in SwiftUI for example!)