0

I have a SwiftUI form that will have multiple pickers on the screen. The first one being select a team from a database stored in Core Data. Each of these teams have a one-to-many relationship with Player. Here are the entities:

Player

enter image description here

Team

enter image description here

In my content view I use a fetch request to get all the teams from Core Data, which I will then iterate through to finish my picker.

@FetchRequest(sortDescriptors: [])
private var teams: FetchedResults<Team>

@State var teamSelected = 0

var body: some View {
    NavigationView{
        Form{
            Section(header: Text("Team Selection")){
                Picker(selection: $teamSelected, label: Text("Team Select")){
                    ForEach(0..<teams.count){ team in
                        Text("\(teams[team].name ?? "Unknown")")
                    }
                }
            }
        }
        .navigationBarTitle("Team Selection")
    }
}

I then want to have multiple pickers below this allowing you to select an individual player from that specific team. I have tried a few ways but never found the correct solution and they all seem way too long winded and hacky. My attempt is below

Picker(selection: $number1, label: Text("#1")){
    ForEach(0..<teams[teamSelected].people!.count){player in
        Text(player.name)
    }
}

Any ideas? I want the second picker to be easily replicable, as I will eventually need it a lot

benpomeroy9
  • 375
  • 5
  • 21

1 Answers1

0

Would the following work?

Picker(selection: $selectedPlayerIndex, label: Text("#1")){
    ForEach(0..<teams[teamSelected].people!.count){playerIndex in
        Text(teams[teamSelected].people![playerIndex].name)
    }
}

So for each player index, you create a Text with the name of the player at that index.

If you will need it a lot, you could create a struct for it:

struct PlayerPicker: View {
    var team: Team
    @Binding var selectedPlayerIndex: Int

    var body: some View {
        Picker(selection: $selectedPlayerIndex, label: Text("#1")){
            ForEach(0..<team.people!.count){playerIndex in
                Text(team.people![playerIndex].name)
            }
        }
    }
}

Then you can use it like this:

@FetchRequest(sortDescriptors: [])
private var teams: FetchedResults<Team>

@State var teamSelected = 0
@State var selectedPlayerIndex: Int

var body: some View {
    NavigationView{
        Form{
            Section(header: Text("Team Selection")){
                Picker(selection: $teamSelected, label: Text("Team Select")){
                    ForEach(0..<teams.count){ team in
                        Text("\(teams[team].name ?? "Unknown")")
                    }
                }

                PlayerPicker(team: teams[teamSelected], selectedPlayerIndex: $selectedPlayerIndex)
            }
        }
        .navigationBarTitle("Team Selection")
    }
}

This is a pretty clean solution; not hacky at all IMHO.
I couldn't really test this code and haven't written in SwiftUI for a good while, so excuse me if I made a mistake somewhere. But you doubtlessly get the idea. :)

cbjeukendrup
  • 3,216
  • 2
  • 12
  • 28