I'm facing a weird issue with contextMenu(forSelectionType:menu:primaryAction:)
attached to a List
. It works fine if you enable edit mode, and start selecting the rows by tapping, but if you have a button that what it does is manually modify the selection, the returned rows when the contextMenu is invoked is incorrect.
Furthermore, if you use the select all button, but actually scroll to the bottom of the list, the returned values is correct, so it seems that unless the cell is rendered, the contextMenu won't return it.
Does anybody know if I'm doing something wrong? Here's a quick example to reproduce the issue:
struct ContentView: View {
let rows = (0..<100).map{ "Row: \($0)" }
@State var selection: Set<String> = []
var body: some View {
List(selection: $selection) {
ForEach(rows, id: \.self) { row in
Text(row).tag(row)
}
}.contextMenu(forSelectionType: String.self) { contextMenuRows in
Button("Number of rows in the contextMenu: \(contextMenuRows.count)") {}
}.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
if selection.isEmpty {
Button("Select All") { selection = Set(rows) }
} else {
Button("Deselect All") { selection = [] }
}
}
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
}
}
}
Make sure to embed the ContentView
inside a NavigationView
to be able to see the navigation bar.
Video demo showing the issue: https://imgur.com/a/fxKk5Cs
- Works fine when selecting manually
- When selecting all only displays the first 9 rows
- After scrolling, all rows are available to the contextMenu