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.
Check out our new book!
Swift Gems
100+ tips to take your Swift code to the next level
Swift Gems
100+ tips to take your Swift code to the next level
- Advanced Swift techniques for experienced developers bypassing basic tutorials
- Curated, actionable tips ready for immediate integration into any Swift project
- Strategies to improve code quality, structure, and performance across all platforms
- Practical Swift insights from years of development, applicable from iOS to server-side Swift