Skip to content

Instantly share code, notes, and snippets.

@krzysztofzablocki
Last active April 5, 2021 14:06
Show Gist options
  • Save krzysztofzablocki/9cdb3ffa14f4ab92a8b2d624efbbbfa3 to your computer and use it in GitHub Desktop.
Save krzysztofzablocki/9cdb3ffa14f4ab92a8b2d624efbbbfa3 to your computer and use it in GitHub Desktop.
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

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