SwiftUI [This is the code for the list and my button]
ForEach(items, id: \.self) { current in
VStack(alignment: .leading) {
Text("\(current.task)")
.padding(2)
Text("\(dateFormatter.string(from: current.date))")
Button(action: {},
label: {
Image(systemName:
"checkmark.rectangle.fill")
.resizable()
.frame(width: 50, height: 35)
})
}
Items is my array containing a string and a date. Here is the code for it:
@State var items:[Tasks] = [Tasks(task: "Test", date: Date())]
And here is Tasks:
struct Tasks: Hashable {
let task: String
let date: Date
}
I want to have a user be able to click a button under each list item and it will remove that list item. I am currently using the onDelete method but I would prefer to have a confirm button in each list item that would allow the user to remove that list item.
@State var counter = -1
I tried using a counter that would increase by 1 each time the ForEach loop ran and create a new variable inside of that ForEach loop equal to it. However I could not access the variable inside of the button to use as an index.
itemsin your code?