2

我正在尝试为用户使用列表选择来从列表中选择零天或多天。我曾尝试使用此问题中列出的解决方案,但无济于事。

我的代码如下所示,并且我已验证编辑模式处于活动状态。我应该期待上面链接的答案中的行为。

代码:

struct EditView: View {

    @State var selectKeeper = Set<String>()
    var weekdays: [String] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
    @Environment(\.editMode) var mode

    var body: some View {

            Form {

                Section(header: Text("Overtime Days")) {

                    List(self.weekdays, id: \.self, selection: $selectKeeper) { day in
                        Text(day)
                    }

                }

            }   .navigationBarTitle(Text("My Title"))
                .padding(.top)
                .onAppear(perform: {
                    print(self.mode?.value as Any)
                })

        }
}

EditButton 包含在父视图中,并在以下代码中启用。

struct JobDetailHost: View {

    @Environment(\.editMode) var mode
    @Binding var jobDetails: JobDetails

    var body: some View {

        VStack {

                if self.mode?.value == .inactive {
                    JobDetailView(jobDetails: jobDetails)
                } else {
                    EditView(jobDetails: $jobDetails)
                        .onDisappear(perform: {
                            //DO STUFF...
                        })
                } 
        }
        .navigationBarItems(trailing: EditButton())
        .onAppear(perform: bindDraft)


    }
}

该表应显示其他答案中的选择行为,但目前它在编辑模式下仅显示为普通列表。

4

2 回答 2

2

Form确实阻止了EditMode的传播(或无论如何正常工作)。唯一的办法是不要将它嵌入到表单中,并希望它在发布之前得到修复(目前是 XcodeBeta5,报告..)。

import SwiftUI

struct SelectionView: View {
    @Binding var jobDetails: JobDetails

    var weekdays: [String] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

    var body: some View {
        VStack{
            List(self.weekdays, id: \.self, selection: $jobDetails.weekdays) { day in
                Text(day)
            }
        }
    }
}

struct JobDetails: Identifiable {
    let id = UUID()
    var weekdays = Set<String>()
}

struct JobDetailsView: View {
    let jobDetails: JobDetails

    var body: some View {
        HStack{
            Text("Weekdays:")
            Text(jobDetails.weekdays.joined(separator: ", "))
        }
    }
}

struct JobDetailHost: View {

    @Environment(\.editMode) var mode
    @Binding var jobDetails: JobDetails

    var isEditing: Bool {
        return mode?.value == .inactive
    }

    var body: some View {

        VStack {
            //HStack{ Spacer(); EditButton().padding() }
            ZStack{
                if isEditing {
                    JobDetailsView(jobDetails: jobDetails)
                } else {
                    SelectionView(jobDetails: $jobDetails)
                }
            }
        }.navigationBarItems(trailing: EditButton())
    }
}

struct JobsView: View {
    @State var jobDetails: [JobDetails] = [
        JobDetails(),
        JobDetails(),
        JobDetails(),
        JobDetails(),
    ]

    var body: some View {
        NavigationView{
            List($jobDetails) { (jobDetail: Binding<JobDetails>) in
                NavigationLink(jobDetail.id.uuidString, destination: JobDetailHost(jobDetails: jobDetail))
            }
        }
    }
}


struct ListSelection2View: View {
    var body: some View {
        JobsView()
    }
}
于 2019-08-16T16:59:54.507 回答
1

我为此挣扎了一会儿,答案不是@Fabian 的想法。

您需要使用.onDelete(),否则不会出现选择气泡:

Form {
    Section(header: Text("Overtime Days")) {
        List(self.weekdays, id: \.self, selection: $selectKeeper) { day in
            Text(day)
        }
        .onDelete(perform: noOp)
    }
}
.navigationBarTitle(Text("My Title"))
于 2019-09-02T12:26:00.960 回答