2

I am trying to use a list selection for the user to zero or more days from a list. I have tried to use the solution listed in this question, but to no avail.

My code is given below, and I have verified edit mode is active. I should expect behaviour like in the answer linked to above.

Code:

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)
                })

        }
}

The EditButton is contained in a parent view and is enabled in the following piece of code.

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)


    }
}

The table should show the selection behaviour as in the other answer, but currently it displays as just a normal list in edit mode.

3
  • 1
    Possible duplicate of How does one enable selections in SwiftUI's List Commented Aug 16, 2019 at 16:21
  • I have linked to this question in my own, it does not solve my issue here as the current top answer is implemented in this code but does not have the intended behaviour. Commented Aug 16, 2019 at 16:26
  • Good point, I didn't realize. Commented Aug 16, 2019 at 17:00

2 Answers 2

2

Form does stop EditMode from propagating (or working correctly anyway). The only way to go is to not embed it inside the Form and to hope it does get a fix before release (currently XcodeBeta5, reporting..).

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()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I struggled with this for a bit and the answer is not what @Fabian thought.

You need to use .onDelete() or else the selection bubbles will not appear:

Form {
    Section(header: Text("Overtime Days")) {
        List(self.weekdays, id: \.self, selection: $selectKeeper) { day in
            Text(day)
        }
        .onDelete(perform: noOp)
    }
}
.navigationBarTitle(Text("My Title"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.