Automatically generated List edit operations
Starting from iOS 16 and macOS 13 SwiftUI List view can automatically generate move and delete operations without the need of onMove
and onDelete
closures.
To create a simple editable list, we can pass a binding to a collection to the List
view and provide a set of edit operations we want to allow.
struct ContentView: View {
@State var items = ["A", "B", "C"]
var body: some View {
NavigationView {
List(
$items, id: \.self,
edits: [.delete, .move]
) { $item in
Text(item)
}
}
}
}
You can see the available options in the documentation for EditOperations.
If we don't want to allow deletion of certain rows, we can use deleteDisabled() modifier on a per row basis.