James Woodcock
James Woodcock

Reputation: 449

SwiftUI List selection does not select

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.

Upvotes: 2

Views: 7959

Answers (2)

caram
caram

Reputation: 1719

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

Upvotes: 1

Fabian
Fabian

Reputation: 5358

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

Upvotes: 2

Related Questions