How can I enable multi-select and then move / reorder selected items in a List with ForEach (in SwiftUI)?
I tried the following code. On Mac it works fine - it allows me to select multiple items, and then drag them all together to another place in the list. On iOS it allows me to move individual items with drag-and-drop, and allows me to enter Edit mode to select multiple items but when I try to move the selected items with drag-and-drop, they can be dragged but they can't be dropped elsewhere in the list:
struct ExampleListView: View {
@State var items = ["Dave", "Tom", "Jeremy", "Luke", "Phil"]
@State var selectedItems: Set<String> = .init()
var body: some View {
NavigationView {
List(selection: $selectedItems) {
ForEach(items, id: \.self) { item in
Text(item).tag(item)
}.onMove(perform: move)
}
.navigationBarItems(trailing: EditButton())
}
}
func move(from source: IndexSet, to destination: Int) {
items.move(fromOffsets: source, toOffset: destination)
}
}
This code has the same behavior:
struct ExampleListView: View {
@State var items = ["Dave", "Tom", "Jeremy", "Luke", "Phil"]
@State var selectedItems: Set<String> = .init()
var body: some View {
NavigationView {
List($items, id: \.self, editActions: .all, selection: $selectedItems) { $item in
Text(item).tag(item)
}
.navigationBarItems(trailing: EditButton())
}
}
func move(from source: IndexSet, to destination: Int) {
items.move(fromOffsets: source, toOffset: destination)
}
}
Hi @chetan51 , this is a known issue in iOS, please file a feedback report at https://feedbackassistant.apple.com in Developer Technologies and SDKs -> iOS -> SwiftUI as that always helps!