import SwiftUI
struct SwipeActionsListView: View {
@State private var items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) { item in
Text(item)
.swipeActions(edge: .trailing) {
Button(role: .destructive) {
withAnimation {
items.removeAll { $0 == item }
}
} label: {
Label("Delete", systemImage: "trash")
}
.tint(.red)
Button {
print("Edit tapped on \(item)")
} label: {
Label("Edit", systemImage: "pencil")
}
.tint(.gray)
}
}
}
.navigationTitle("Swipe Actions List")
}
}
}
struct SwipeActionsListView_Previews: PreviewProvider {
static var previews: some View {
SwipeActionsListView()
}
}This solution uses SwiftUI's swipeActions modifier on each list item to add swipe gestures.
The Delete button has a destructive role and red tint. When tapped, it removes the item from the list with animation.
The Edit button has a gray tint and prints a message to the console identifying which item was tapped.
The list updates dynamically because the items array is marked with @State.