Reputation: 414
I'm trying to implement a delete feature for a list of elements but the onDelete event doesn't work as expected.
Here's the code:
@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()
}
}
The error I receive is:
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'
Implementing the List was tricky, but the code seems working allowing saving the modifications in the data manager.
Upvotes: 3
Views: 369
Reputation: 385590
The onDelete
modifier is a method of ForEach
.. It is not defined for other View
s like List
. You need to modify your code to use a ForEach
inside your List
.
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)
}
Technically, onDelete
is an extension method of the DynamicViewContent
protocol, but the only types that conform to DynamicViewContent
are ForEach
and modified derivatives of ForEach
.
Upvotes: 3