Beginning Table Views · Moving Items Between Sections | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5995-beginning-table-views/lessons/55

Hello Mr. Moakley, could you please explain the logic behind the modifications you have made within the addToDo method regarding the indexes ?

func addTodo(_ item: ChecklistItem, for priority: Priority, at index: Int = -1) {
switch priority {
case .high:
if index < 0 {
highPriorityTodos.append(item)
} else {
highPriorityTodos.insert(item, at: index)
}

Hey there, essentially, if an index isn’t specified, we append the todo item to the array. This will always put the item at the last place. Otherwise, we insert per the argument.

1 Like

At the end of this tutorial, we update moveRowAt and call tableView.reloadData() at the end of the method. When it’s called and a row is moved, the editing view is shown upon releasing the cell. The item that it is trying to edit is the item at the index where the moved cell came from. So if I grabbed the 4th item in the list (index: 3), no matter where I move it, the item that ends up being at index: 3 will go into editing mode. If I comment out tableView.reloadData(), it stops doing that. I’m on the GM Xcode 11, is it possible something changed causing this bug?

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

how about using Int? instead of Int( with default value -1 )

func addTodo(_ item: ChecklistItem, for priority: Priority, at index: Int? = nil)

@bdmoakley Do you have any feedback about this? Thank you - much appreciated! :]

Just eyeballing it, that’s a good approach in tune with the language. I still have Java habits whereby failed searches always returned -1