Post

Replies

Boosts

Views

Activity

Reply to How to reorder list items with Swift Data?
I added a property called order to the types I want to move. here is my moveList method which is called when a user moves a list item in my app. There's still an issue when moving items down but it works for the most part. private func moveList(from source: IndexSet, to destination: Int) { guard searchText.isEmpty, filteredLists.lists.count > 1 else { return } debugPrint("Source: \(source), destination: \(destination)") let sourceList = getList(at: source) debugPrint("Source List: \(sourceList.name) to position \(destination)") let dropIndex = destination <= 0 ? 0 : destination < filteredLists.lists.count ? destination : destination - 1 debugPrint("dropIndex: \(dropIndex)") let dropList = filteredLists.lists[dropIndex] let isMovingUp = destination < sourceList.order dropList.order = isMovingUp ? dropList.order + 1 : dropList.order - 1 debugPrint("Drop Location: \(dropList.name), position: \(dropList.order)") sourceList.order = destination debugPrint("Source List: \(sourceList.name) to position \(sourceList.order)") reorderLists() }
Dec ’23