Существует 2 основных изменения и 1 недостаток swiftUI.
Обновлен метод moveListItem
Создана альтернатива для изменения компаний путем перемещение с помощью NavigationalLink
SwiftUI не имеет возможности перемещаться между группами в GroupedList с помощью .onMove()
Приложение работает технически. Но не так, как вы предполагали, если Apple не добавит эту функцию. Есть еще одна опция, которая заключается в создании пользовательского представления списка с помощью пользовательского метода перемещения, который совершенно отличается от topi c.
import SwiftUI
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")]),
Company(name: "Facebook", employees: [Person(name:"Mark"), Person(name:"Sandy")])
]
func deleteListItem(whichElement: IndexSet, from company: Company) {
if let index = self.companies.firstIndex(of: company) {
self.companies[index].employees.remove(atOffsets: whichElement)
}
}
func moveListItem(whichElement: IndexSet, to companyIndex: Int, from company: Company) {
if let index = self.companies.firstIndex(of: company) {
self.companies[index].employees.move(fromOffsets: whichElement, toOffset: companyIndex)
}
}
}
struct TestView: View {
@EnvironmentObject 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
NavigationLink(destination: EditEmployee(company: company, employee: employee)){
Text(employee.name)
}.id(UUID())
}
.onDelete { (indexSet) in
self.companyList.deleteListItem(whichElement: indexSet, from: company)
}
.onMove { indexSet, intValue in
self.companyList.moveListItem(whichElement: indexSet, to: intValue, from: company)
}
.onInsert(of: ["chris"]) { (intValue, _) in
print("wtf")
}
}
}
}
.listStyle(GroupedListStyle())
.navigationBarItems(trailing: EditButton())
}
}
}
}
struct EditEmployee: View {
@EnvironmentObject var companyList: CompanyList
var company: Company
var employee: Person
var body: some View {
VStack(alignment: .leading) {
Text("Company")
Picker(selection: Binding<Company>(
get: { () -> Company in
return self.company
}, set: { (company) in
if let cid = self.companyList.companies.firstIndex(of: self.company) {
if let eid = self.companyList.companies[cid].employees.firstIndex(of: self.employee) {
if let ncid = self.companyList.companies.firstIndex(of: company) {
self.companyList.companies[cid].employees.remove(at: eid)
self.companyList.companies[ncid].employees.append(self.employee)
}
}
}
}
), label: Text("")){
ForEach(self.companyList.companies) { company in
Text(company.name).tag(company)
}
}.pickerStyle(SegmentedPickerStyle())
Spacer()
}.padding()
.navigationBarTitle(self.employee.name)
}
}