如何在 SwiftUI 中删除动态列表中的复杂项目?

how is possible deleting complex items in a dynamic list in SwiftUI?

我正在尝试为元素列表实现删除功能,但 onDelete 事件没有按预期工作。

代码如下:

    
    @ObservedObject var dm: DataManager = DataManager.shared
    @State var isAddShown = false
    
    var body: some View {
        NavigationView {
            VStack {
                List($dm.TTDItemList) { $item in
                    VStack(alignment: .leading) {
                        TextEditor(text: $item.itemDesc)
                            .onTapGesture {}
                        Text(item.ItemTagsToText())
                            .font(.caption)
                            .foregroundColor(Color.red)
                            .multilineTextAlignment(.leading)
                    }
                    
                }
                .onDelete(perform: cancella)
            }
            .onTapGesture { hideKeyboardAndSave() }

            .navigationBarTitle(Text("Board"), displayMode: .inline)
            .navigationBarItems(trailing:
                                    Button(action: { withAnimation { self.isAddShown.toggle() } }) {
                Image(systemName: !isAddShown ? "plus.circle.fill" : "minus.circle.fill").imageScale(.large)
            }
            )
        }
    }
    
    private func hideKeyboardAndSave() {
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        dm.salva()
    }
    
    func cancella(at offset: IndexSet) {
        guard let intindex = Array(offset).first else { return }
        dm.TTDItemList.remove(at: intindex)
        dm.salva()
    }
    
    
}

我收到的错误是:

Value of type 'List<Never, ForEach<LazyMapSequence<Array.Indices, (Array.Index, UUID)>, UUID, VStack<TupleView<(some View, some View)>>>>' (aka 'List<Never, ForEach<LazyMapSequence<Range, (Int, UUID)>, UUID, VStack<TupleView<(some View, some View)>>>>') has no member 'onDelete'

实施列表很棘手,但代码似乎可以在数据管理器中保存修改。

The onDelete modifier is a method of ForEach.。它没有为 List 等其他 View 定义。您需要修改代码以在 List.

中使用 ForEach
            List {
                ForEach($dm.TTDItemList) { $item in
                    VStack(alignment: .leading) {
                        TextEditor(text: $item.itemDesc)
                            .onTapGesture {}
                        Text(item.ItemTagsToText())
                            .font(.caption)
                            .foregroundColor(Color.red)
                            .multilineTextAlignment(.leading)
                    }
                    
                }
                .onDelete(perform: cancella)
            }

从技术上讲,onDeleteDynamicViewContent 协议的扩展方法,但唯一符合 DynamicViewContent 的类型是 ForEach 和 [=12] 的修改派生=].