Implementing the data source methods

There are two required methods to implement for UICollectionViewDataSource. The first method will tell the collection view whether it's okay for a certain item to be moved around. The second is responsible for updating the underlying data source based on the new cell order. Let's jump to the implementation right away as follows:

func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool { 
return true
}

func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let movedContact = contacts.remove(at: sourceIndexPath.row)
contacts.insert(movedContact, at: destinationIndexPath.row)
}

These implementations should be fairly straightforward. If asked whether an item can move, the return value is true because in this app all items can move. Also, updating the order in the data is done just the same as it was done in the previous chapter.