GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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 Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.