[英]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'我尝试使用特定的 function 添加.onDelete 属性,但出现错误: 'List<Never, ForEach<Range<Array<[AnnData]>.Index>, Range<Array<[AnnData]>.Index> 类型的值。 Element, ForEach<Range<Array.Index>, Range<Array.Index>.Element, Text>>>' (又名'List<Never, ForEach<Range, Int, ForEach<Range, Int, Text>>>')没有成员“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?如果我不允许使用.onDelete,我可以使用什么?
Thanks for all your support.感谢您的支持。
Use inside the List.在列表内使用。 Give onDelete to ForEach.
将 onDelete 交给 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.