I have the same use case where I only want rows to be selectable in edit mode. I solved this by checking whether edit mode was active before passing the selection binding to the List.
For example:
struct ContentView: View {
let items: [Item]
@Environment(\.editMode)
private var editMode
@State
private var selectedItemIDs = Set<Item.ID>()
var body: some View {
NavigationStack {
List(items, selection: isEditModeActive ? $selectedItemIDs : nil) { item in
ItemView(item: item)
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
EditButton()
}
}
}
}
private var isEditModeActive: Bool {
return editMode?.wrappedValue == .active
}
}
This approach works. However, be aware that it does currently trigger a SwiftUI bug where the List no longer shows its multi-select checkmarks in edit mode. I reported the bug in FB13434460 (Open Radar ID 5544045669515264).
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: