TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: MultiSelect doesn't working

Forums > SwiftUI

@HRait  

Hi I need your Help, I want to have a multiselection in my List but its not working. Do I do sth wrong?

@State private var selectedKosten = Set<UUID>()

...
Section("Personen") {
                HStack {
                    TextField("Füge eine Person hinzu:", text: $newPersonName)

                    Button("Hinzufügen", action: addSight)
                        .foregroundStyle(.purple)
                }
                List {
                    ForEach(destination.personen) { personen in
                        NavigationLink() {
                            //List {
                                List(destination.kosten, selection: $selectedKosten) { kosten in
                                    Text(kosten.name)
                                }
                            Text("\(selectedKosten.count) ausgewählt")
                            //}
                        } 
                        label: {
                            Text(personen.name)
                        }
                    }
                    .onDelete(perform: deletePersonen)
                }
            }
...

1      

I find in my lists (which are not as complicated as above), that adding ".tag( item)" helps.

Text( kosten.name ).tag( kosten.id )

1      

@HRait  

Dont know why I cant choose anything, inside the Personen I cant choose kosten what can I do?

1      

Simplify it until you can.

I see a list inside a ForEach, inside a Navigation Link, inside a list.

Strip it back, so you only have the

List(destination.kosten, selection: $selectedKosten) { kosten in
                                    Text(kosten.name)
                                }

left. Make sure you try adding the .tag to the Text( ). If it works, bring back the rest until it doesn't work any more.

One thing I would say is I don't know if a list inside a list is supported, it already gets funky with a list inside of a ScrollView.

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.