Parcker
Parcker

Reputation: 219

.ondelete SwiftUI List with Sections

I cannot release method for delete and moving rows in SwiftUI list with sections.

I have model for Category:

struct Category: Identifiable {

    var id = UUID()
    var title: String
    var number: Int
    var items: [ChecklistItem]

    func deleteListItem(whichElement: IndexSet) {
      items.remove(atOffsets: whichElement)
    }

    func moveListItem(whichElement: IndexSet, destination: Int) {
      items.move(fromOffsets: whichElement, toOffset: destination)
    }


}

ChecklistItem:

struct ChecklistItem: Identifiable {
  
  let id = UUID()
  var name: String
  var isChecked = false
    
}

And Checklist:

class Checklist: ObservableObject {

  @Published var items = [Category]()
}

This is my View for List:

struct ChecklistView: View {

  @EnvironmentObject var checklist: Checklist
  @State var newChecklistItemViewIsVisible = false

  var body: some View {
    NavigationView {
      List {
        ForEach(checklist.items) { category in
            Section(header: Text(category.title)) {
                ForEach(category.items) { item in
                    HStack {
                      Text(item.name)
                      Spacer()
                      Text(item.isChecked ? "✅" : "🔲")
                    }
                    .background(Color.white)
                    .onTapGesture {
                        if let matchingIndex =
                            checklist.items[category.number].items.firstIndex(where: { $0.id == item.id }) {
                            checklist.items[category.number].items[matchingIndex].isChecked.toggle()
                      }
                    }
                }
                .onDelete(perform: checklist.items[category.number].deleteListItem)
                .onMove(perform: checklist.items[category.number].moveListItem)
            }
        }
      }
      .navigationBarItems(
        leading: Button(action: {
            self.newChecklistItemViewIsVisible = true

        }) {
          HStack {
            Image(systemName: "plus.circle.fill")
            Text("Add")
          }
        },
        trailing: EditButton()
      )
      .navigationBarTitle("List")
    }
    .onAppear {
        //print("ContentView appeared!")
    }
    .sheet(isPresented: $newChecklistItemViewIsVisible) {
      NewChecklistItemView(checklist: self.checklist)
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
      ChecklistView()

  }
}

I cannot use methods .ondelete and .onmove as I cannot use mutating methods in struct. How I can change my code for adding features for deleting and moving items in List with Sections?

Upvotes: 2

Views: 1518

Answers (1)

Asperi
Asperi

Reputation: 257749

You need to use mutating modifier for your functions, and updated code

struct Category: Identifiable {

    // ... other code

    mutating func deleteListItem(_ whichElement: IndexSet) {
      items.remove(atOffsets: whichElement)
    }

    mutating func moveListItem(_ whichElement: IndexSet, _ destination: Int) {
      items.move(fromOffsets: whichElement, toOffset: destination)
    }
}

and usage like

.onDelete { indexSet in 
   checklist.items[category.number].deleteListItem(indexSet) 
}
.onMove { indexSet, dest in 
   checklist.items[category.number].moveListItem(indexSet, dest) 
}

Upvotes: 2

Related Questions