问题描述
如何在分组列表中实现 onMove 函数?不幸的是,我看不到从哪里获得到公司"的信息...
how do i implement the onMove function in a grouped list? unfortunately i cannot see where i get the "to company" information from...
代码如下:
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) {
let index = companies.firstIndex(of: company)!
companies[index].employees.remove(atOffsets: whichElement)
}
func moveListItem(whichElement: IndexSet, to companyIndex: Int) {
print(whichElement.first)
if whichElement.count > 1 {
whichElement.dropFirst()
print(whichElement.first)
}
print(companyIndex)
// let employee = companies[index].employees[whichElement.first!]
// companies[index].employees.remove(at: whichElement.first!)
//
}
}
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.companyList.deleteListItem(whichElement: indexSet, from: company)
}
.onMove { indexSet, intValue in
self.companyList.moveListItem(whichElement: indexSet, to: intValue)
}
.onInsert(of: ["chris"]) { (intValue, _) in
print("wtf")
}
}
}
}
.listStyle(GroupedListStyle())
.navigationBarItems(trailing: EditButton())
}
}
}
}
推荐答案
有 2 个主要变化和 1 个 swiftUI 缺点.
There is 2 main changes and 1 swiftUI drawback.
更新方法
moveListItem
通过使用 NavigationalLink 移动,创造了更换公司的替代方案
Created a alternative to change companies by moving with a NavigationalLink
SwiftUI 无法使用 .onMove()
SwiftUI have no option to move between groups in GroupedList with .onMove()
该应用在技术上有效.但除非 Apple 添加该功能,否则不会如您所愿.还有一个选项,那就是使用自定义移动方法创建自定义列表视图,这是一个完全不同的主题.
The app works technically. But not as you intended unless Apple add the feature. There is one more option, that is to create custom list view with custom move method, which is entirely a different topic.
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)
}
}
这篇关于如何在swiftui中的分组列表上实现移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!