Gold Challenge: Really Preventing the Reordering

For the last Challenge of this unit, we are going to use a delegate method from DataSource, it is invoked at time when the UITableView is going to move a row before that the method tableView(_ tableView: UITableView, moveRowAt…) had been called.

 override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
    if (proposedDestinationIndexPath.row >= itemStore.allItems.count){
        return sourceIndexPath
    }
    return proposedDestinationIndexPath
}

Now the challenge it’s done.

Note: You need implement my Silver Challenge first.

I went about this slightly differently. If you try to move it below the “No more items” row, I want it to go to the row just above that, so I added:

    override func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath {
    var modifiedPath:IndexPath = proposedDestinationIndexPath
    
    if modifiedPath.row == itemStore.allItems.count - 1 {
        modifiedPath.row -= 1
        return modifiedPath
    } else {
        return modifiedPath
    }
    
}
1 Like