Reputation: 1850
I have the following code:
struct FirstView: View {
@State var idArray: Array<String> = ["a", "b", "c"]
var body: some View {
SecondView(idArray: $idArray)
}
}
struct SecondView: View {
@Binding var idArray:Array<String>
var body: some View {
List(){
Section{
ForEach(0..<self. idArray.count, id: \.self){id in
Text(self.idArray[id])
}.onDelete(perform: deleteItem)
}
}
}
func deleteItem(at offsets: IndexSet) {
self.idArray.remove(atOffsets: offsets)
}
}
The problem: When performing a delete it does not delete the right element but rather the last one.
I think that the @Binding
is the issue here as before I did not have the List as an own component and everything in one struct and it worked. The own component would help reduce code redundancy in the future.
Upvotes: 0
Views: 171
Reputation: 265
List {
Section{
ForEach(self.idArray, id: \.self){ id in
Text(id)
}.onDelete(perform: deleteItem)
}
}
Upvotes: 2