I am trying to create a swipe action where I can delete (Ideally trying to have a delete and edit button). I am having an issue with deleting under the swipe action.
I should note that I have class with an array of dates. I would like the user to have the option to delete a date that is in the array
List {
ForEach((0..<$A.Date.count).reversed(), id: \.self) {
i in (Text("\(String(format: "%02d",i+1)) - \(A.Date[i], style: .date)"))
.swipeActions(edge: .trailing) {
**Button (role: .destructive) {
DeleteDate(dateToDelete: i) // Not Working
print("delete")
}** label: {
Label("Delete", systemImage: "trash")
}
.tint(.red)
private func DeleteDate(dateToDelete: Int) {
A.Date.remove(at: dateToDelete)
print("delete function was used")
}
I tried a few functions and an .onDelete. The OnDelete works but currently will not allow me to add buttons if i try the swipe action.
Dates
, and how you declareA
in your View. You should consider using the more robustForEach(dates){ date in ...}
, wheredates
is an array ofIdentifiable
elements. See also the less desirable alternative enumerated Note, use the common naming practice for classes, properties and functions.