【问题标题】:SwiftUI delete and move functionalitySwiftUI 删除和移动功能
【发布时间】:2020-05-04 11:13:52
【问题描述】:

我的移动和删除方法遇到了一些问题。这是对这个问题的跟进:SwiftUI Section from attribute of a struct

我正在尝试按公司对人员进行分组,上一个问题中提供的解决方案效果很好。它确实对我的移动和删除方法有影响,我发现很难弄清楚原因。

delete 函数似乎正在删除我没有选择的行,并且 move 方法崩溃并显示 Attempt to create two animations for cell.

struct Person: Identifiable {
    var id = UUID()
    var name: String
    var company: String
}

class PeopleList: ObservableObject {

    @Published var people = [
        Person(name: "Bob", company: "Apple"),
        Person(name: "Bill", company: "Microsoft"),
        Person(name: "Brenda", company: "Apple"),
        Person(name: "Lucas", company: "Microsoft"),
    ]

    func getGroups() -> [String] {

        var groups : [String] = []

        for person in people {
            if !groups.contains(person.company) {
                groups.append(person.company)
            }
        }
        return groups
    }

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

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

struct  ContentView: View {
    @ObservedObject var peopleList = PeopleList()

    var body: some View {
        NavigationView {
            List () {
                ForEach (peopleList.getGroups(), id: \.self) { group in
                    Section(header: Text(group)) {
                        ForEach(self.peopleList.people.filter { $0.company == group }) { person in

                            Text(person.name)
                        }
                        .onDelete(perform: self.peopleList.deleteListItem)
                        .onMove(perform: self.peopleList.moveListItem)
                    }
                }
            }
            .listStyle(GroupedListStyle())
            .navigationBarItems(trailing: EditButton())
        }
    }
}

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

【问题讨论】:

    标签: swift swiftui


    【解决方案1】:

    更新的答案 - 现在有新的数据模型和有效的删除

    试试这个:

    struct Person: Identifiable, Hashable {
        var id = UUID()
        var name: String
    }
    
    struct Company : Identifiable, Hashable {
    
        var id = UUID()
        var name: String
        var employees : [Person]
    }
    
    class CompanyList: ObservableObject {
    
        @Published var companies = [
            Company(name: "Apple", employees: [Person(name:"Bob"), Person(name:"Brenda")]),
            Company(name: "Microsoft", employees: [Person(name:"Bill"), Person(name:"Lucas")])
        ]
    
        func deleteListItem(whichElement: IndexSet, from company: Company) {
    
            let index = companies.firstIndex(of: company)!
    
            companies[index].employees.remove(atOffsets: whichElement)
        }
    
    //    func moveListItem(whichElement: IndexSet, destination: Int) {
    //        companies.employees.move(fromOffsets: whichElement, toOffset: destination)
    //    }
    }
    
    struct  ContentView: View {
        @ObservedObject var companyList = CompanyList()
        @State var text : String = ""
    
        var body: some View {
            NavigationView {
                VStack {
                    List () {
                        ForEach (companyList.companies, id: \.self) { company in
                            Section(header: Text(company.name)) {
                                ForEach(company.employees) { employee in
    
                                    Text(employee.name).id(UUID())
                                }
                                .onDelete { (indexSet) in
                                    self.text = ("\(indexSet), \(indexSet.first)")
                                    self.companyList.deleteListItem(whichElement: indexSet, from: company)
                                }
    
                                //    .onMove(perform: self.companyList.moveListItem)
                            }
                        }
                    }
                    .listStyle(GroupedListStyle())
                    .navigationBarItems(trailing: EditButton())
                    Text(text)
                }
            }
        }
    }
    

    【讨论】:

    • 太好了,这很清楚。移动功能似乎确实存在一些问题。每当我在一个部分移动某人时,他们也会在另一部分移动。你也看到了吗?
    • 是的......好吧......移动真的没有意义,如果我们对它们进行排序;)另一个问题 - 我认为 - 是过滤器结果是“未定义” - 甚至更糟 -移动到其他部分 -> 不能工作,因为那样你也必须更改公司名称....
    • 如果你真的想重新排序,你必须改变你的数据模型——我认为。
    • 感谢克里斯的所有帮助。我会考虑一下我的数据模型,但我认为我可以不重新排序。 I just looked again and am running into an issue where Brenda is deleted when Lucas is selected to be deleted.我没有更改您以前的代码中的任何内容,所以我想知道您是否也看到了?比尔和卢卡斯在被删除时都不会被删除,而是来自 Apple 部分的相应内容会被删除。
    • 不,我没有这个问题……你能不能把它拍成电影来放映?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多