I'm designin a View to store and show some strings written by the user, and I would like to add an edit button in the toolbar in order to eliminate only the choosen strings.
I tried adding .onDelete property with an specific function but I get the error: Value of type 'List<Never, ForEach<Range<Array<[AnnData]>.Index>, Range<Array<[AnnData]>.Index>.Element, ForEach<Range<Array.Index>, Range<Array.Index>.Element, Text>>>' (aka 'List<Never, ForEach<Range, Int, ForEach<Range, Int, Text>>>') has no member 'onDelete'
My code is the following:
struct annotationsView: View {
@State private var text = ""
// annotations
@State var annotations : [[AnnData]] = [[]]
var body: some View {
VStack{
Form{
HStack{
TextField("Add your annotations here", text: $text)
.textFieldStyle(RoundedBorderTextFieldStyle())
Button("Submit") {
annotations[annotations.count - 1].append(AnnData(Anntext: text))
self.hideKeyboard()
text = ""
}
}
List{
ForEach(annotations.indices, id:.self){index in
ForEach(annotations[index].indices, id:.self){annotationIndex in
Text(annotations[index][annotationIndex].Anntext)
}
}
}
.onDelete(perform: self.deleteItem)
}
}
.background(
Image("Background")
.resizable()
.edgesIgnoringSafeArea(.all)
.navigationBarTitle(Text("Annotations"), displayMode: .inline)
)
.navigationBarItems(trailing: EditButton())
}
private func deleteItem(at indexSet: IndexSet) {
self.annotations.remove(atOffsets: indexSet)
}
}
struct AnnData : Identifiable{
var id = UUID().uuidString
var Anntext: String}
If I'm not allowed to use .onDelete, what can I use?
Thanks for all your support.
question from:
https://stackoverflow.com/questions/65883738/how-can-i-delete-item-from-a-list-with-ondelete-in-switui 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…