This keeps selection on macOS, but not on iPad.
On iPad each row in the List needs to be NavigationLink, no need for .tag. The selection is not updated, need to manually update with onTapGesture
For List selection to work, make sure
selection binding type matches. Preferablly using ID than Self
Need to specify id in ForEach for List to infer tag. Otherwise, need to specify tag for cell
struct Sidebaer: View {
class ViewModel: ObservableObject {
@Published var group: BookGroup?
}
@StateObject private var vm = ViewModel()
var body: some View {
List(selection: $vm.group) {
allView
foldersView
tagsView
}
Note that
Double-tap will cancel default List selection
onDrag will interfere with onInsert and selelction
List(selection: $selectedItem) {
ForEach(manager.stack.items) { item in
ItemCell(item: item)
.onDrag {
NSItemProvider(object: item.text as NSString)
}
.tag(item)
}
.onMove { source, destination in
manager.stack.items.move(fromOffsets: source, toOffset: destination)
}
.onDelete { indexSet in
manager.stack.items.remove(atOffsets: indexSet)
}
.onInsert(of: [.text, .url]) { index, itemProviders in
manager.handleInsert(at: index, itemProviders: itemProviders)
}
}
Specify optional value for
List(selection:)
.This keeps selection on macOS, but not on iPad. On iPad each row in the List needs to be
NavigationLink
, no need for.tag
. Theselection
is not updated, need to manually update withonTapGesture
For List selection to work, make sure
id
in ForEach for List to infertag
. Otherwise, need to specifytag
for cellNote that