I am new on learning the Apple Book named APP Development with Swift. And I found a confused point about the Re-ordering the tableView, which was written by Apple Book.
Code is as below: Emoji is a struct, emojis is an var, emojis: [Emoji]
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
let movedEmoji = emojis.remove(at: fromIndexPath.row)
emojis.insert(movedEmoji, at: to.row)
tableView.reloadData()
}
What my confusion is: Even though I deleted the code inside the "override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath)", I can still use a button to enter the editing condition, then use the Re-order control to move the items up or down. Therefore, what is the usage of the code in the middle? I mean below code:
let movedEmoji = emojis.remove(at: fromIndexPath.row)
emojis.insert(movedEmoji, at: to.row)
tableView.reloadData()
I think the code is just to move the item in the tableview, it's like first remove then insert, so is it unnecessary?
emojisarray act as a data source of the table view, and that it should be kept consistent with what's displayed in the table view? - Sweeper