2

I followed this document (and this one) to add a delete feature to my list in an app using SwiftUI. Both pages say that once you add the .onDelete(perform: ...) piece of code you will be able to swipe and get a Delete button. Nevertheless this is not what I see. The code compiles but I see nothing on swipe.

My list is backed up by code like this:

@FetchRequest(
    entity: ...,
    sortDescriptors: []
) var myList: FetchedResults<MyEntity>

and not by @State. Could this be an issue?

Below follows more of the relevant code, in case this may be useful:

private func deleteSpot(at index: IndexSet) {
    print(#function)
}

.........

var body: some View {
    VStack {
        ForEach(self.myList, id: \.self.name) { item in
            HStack {
                Spacer()
                Button(action: {
                    self.showingDestinList.toggle()
                    .....
                    UserDefaults.standard.set(item.name!, forKey: "LocSpot")
                }) {
                    item.name.map(Text.init)
                        .font(.largeTitle)
                        .foregroundColor(.secondary)
                }
                Spacer()
            }
        }.onDelete(perform: deleteSpot)
    }
2
  • 1
    Minimal reproducible example? Commented Jul 23, 2020 at 3:09
  • @Asperi, I just added some relevant code in the post, hoping this may be of some use. Commented Jul 23, 2020 at 3:27

2 Answers 2

2

The delete on swipe for dynamic container works only in List, so make

var body: some View {
    List {              // << here !!
        ForEach(self.myList, id: \.self.name) { item in
            HStack {

              // ... other code

            }
        }.onDelete(perform: deleteSpot)
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Indeed you are right. I just found the same answer after losing quite a bit of time. Thanks.
0

By searching and trying various options, I ended up by finding the issue. I find the solution somewhat ridiculous, but to avoid other people to lose time, here it is:

The last part of the code in the post needs to be modified like the following in order to work.

var body: some View {
    VStack {
      List {
        ForEach(self.myList, id: \.self.name) { item in
            HStack {
                Spacer()
                Button(action: {
                    self.showingDestinList.toggle()
                    .....
                    UserDefaults.standard.set(item.name!, forKey: "LocSpot")
                }) {
                    item.name.map(Text.init)
                        .font(.largeTitle)
                        .foregroundColor(.secondary)
                }
                Spacer()
            }
        }.onDelete(perform: deleteSpot)
      }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.