Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Why isn't last delete refreshing the list? Mac example
@main
struct ListBugApp: App {
var body: some Scene {
WindowGroup {
NavigationView { // comment out navigation view and list works
ContentView()
Text("Detail")
}
}
}
}
struct ContentView: View {
@State
var items = ["Delete", "All", "Items"]
@State
var selection: String?
var body: some View {
List(selection: $selection) {
ForEach(items, id: \.self) { item in
Text(item)
.tag(item)
.contextMenu {
Button(action: {
if let idx = self.items.firstIndex(of: item) {
self.items.remove(at: idx)
} else {
print("Item doesn't exist")
}
}){
Text("Delete")
}
}
}
}
}
}
@darrarski
Copy link

darrarski commented Apr 5, 2021

Looks like a bug 😞

The simplest workaround I found to be working is to add an extra view to the List and hide it:

var body: some View {
  List(selection: $selection) {
    ForEach(items, id: \.self) { /* ... */ }
    Divider().hidden() // workaround for the list not being refreshed when last item is deleted
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment