Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

ios - How can I delete item from a list with .onDelete in SwitUI

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use inside the List. Give onDelete to ForEach. Like this

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) //<-- Here
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...