UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.