我无法发布用于删除和移动SwiftUI列表中带有部分的行的方法。
我有用于类别的模型:
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
}
和清单:
class Checklist: ObservableObject {
@Published var items = [Category]()
}
这是我的列表视图:
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()
}
}
我无法使用.ondelete和.onmove方法,因为无法在struct中使用变异方法。如何更改我的代码以添加用于删除和移动带有节的列表中的项目的功能?
答案 0 :(得分:1)
您需要为函数使用mutating
修饰符,并更新代码
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)
}
}
和用法类似
.onDelete { indexSet in
checklist.items[category.number].deleteListItem(indexSet)
}
.onMove { indexSet, dest in
checklist.items[category.number].moveListItem(indexSet, dest)
}