I think the main error comes from a missing NavigationStack in RosaView.
And you should have rosa
In addition, I had to test on an iPad in order for Table to show columns names and all columns (see here: https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column)
Finally, I made a few changes before it works ok. Please test and tell if that works for you, otherwise, explain what is failing.
struct Rosa: Identifiable, Codable, Hashable, Equatable {
var id = UUID()
let stagione: String
let nomeGiocatore: String
let cognomeGiocatore: String
let nascitaGiocatore: String
let etàGiocatore: Int
let ruoloGiocatore: String
init(stagione: String, nomeGiocatore: String, cognomeGiocatore: String, nascitaGiocatore: String, etàGiocatore: Int, ruoloGiocatore: String) {
self.stagione = stagione
self.nomeGiocatore = nomeGiocatore
self.cognomeGiocatore = cognomeGiocatore
self.nascitaGiocatore = nascitaGiocatore
self.etàGiocatore = etàGiocatore
self.ruoloGiocatore = ruoloGiocatore
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
guard let birthDate = dateFormatter.date(from: nascitaGiocatore) else { return}
let currentYear = Calendar.current.component(.year, from: Date())
let birthYear = Calendar.current.component(.year, from: birthDate)
_ = currentYear - birthYear
}
static func testRosa() -> [Rosa] {
return [
Rosa(stagione: "2023/2024", nomeGiocatore: "Matt", cognomeGiocatore: "Bar", nascitaGiocatore: "31-03-2000", etàGiocatore: 24, ruoloGiocatore: "Portiere"),
Rosa(stagione: "2023/2024", nomeGiocatore: "Fabio", cognomeGiocatore: "Bel", nascitaGiocatore: "30-11-1982", etàGiocatore: 41, ruoloGiocatore: "Difensore"),
Rosa(stagione: "2023/2024", nomeGiocatore: "Ale", cognomeGiocatore: "Nev", nascitaGiocatore: "23-04-2001", etàGiocatore: 23, ruoloGiocatore: "Attaccante"),
Rosa(stagione: "2022/2023", nomeGiocatore: "Matte", cognomeGiocatore: "Repe", nascitaGiocatore: "28-02-1999", etàGiocatore: 25, ruoloGiocatore: "Centrocampista")
]
}
}
// Run on iPad: See https://stackoverflow.com/questions/75277014/swift-table-only-show-1-column
struct RosaView: View {
@State var rosa: [Rosa] = Rosa.testRosa()
@State private var apriNuovoGiocatore = false
@State var stagione: String = "2023/2024"
var rosaFiltrata: [Rosa] {
rosa.filter { // <<-- replace Rosa.testRosa()
$0.stagione == stagione
}
}
@State private var selezioneGiocatore: Set<Rosa.ID> = []
@State private var ordine = [KeyPathComparator(\Rosa.ruoloGiocatore)]
var body: some View {
NavigationStack { // <<-- ADD THIS
VStack(alignment: .leading) {
Text("Stagione: \(stagione)")
.fontWeight(.bold)
.font(.headline)
.foregroundColor(.blue)
.padding()
Table(rosaFiltrata, selection: $selezioneGiocatore, sortOrder: $ordine) {
TableColumn("Nome", value: \.nomeGiocatore) // REMOVE Text( and .foregroundStyle(.blue)
TableColumn("Cognome", value: \.cognomeGiocatore)
TableColumn("Ruolo", value: \.ruoloGiocatore)
TableColumn("Data di nascita", value: \.nascitaGiocatore)
TableColumn("Età") {
rosa in
Text("\(rosa.etàGiocatore)")
}
.width(100)
}
}
.frame(width: 700, height: 300)
.toolbar {
Button {
apriNuovoGiocatore = true
} label: {
Image(systemName: "person.badge.plus")
.foregroundColor(.blue)
}
.sheet(isPresented: $apriNuovoGiocatore, content: {
NuovoGiocatore(rosaArray: $rosa, nomeNuovoGiocatore: "", cognomeNuovoGiocatore: "", nascitaNuovoGiocatore: "", ruoloNuovoGiocatore: "", etàNuovoGiocatore: 20) // <<-- pass $rosa as parameter to a Binding
})
}
.navigationTitle("Rosa")
}
}
}
struct NuovoGiocatore: View { // should start with uppercase
@Environment(\.dismiss) var dismiss
@Binding var rosaArray : [Rosa] // <<-- ADD THIS
@State var nomeNuovoGiocatore: String = ""
@State var cognomeNuovoGiocatore: String = ""
@State var nascitaNuovoGiocatore: String = ""
@State var ruoloNuovoGiocatore: String = ""
@State var etàNuovoGiocatore: Int = 20
var body: some View {
NavigationStack {
Form {
TextField("Nome:", text: $nomeNuovoGiocatore)
TextField("Cognome:", text: $cognomeNuovoGiocatore)
}
.navigationTitle("Nuovo giocatore")
.toolbar {
Button("Cancel") {
dismiss()
}
Button("Aggiungi giocatore") {
let nuovoGiocatore = Rosa(stagione: "2023/2024", nomeGiocatore: nomeNuovoGiocatore, cognomeGiocatore: cognomeNuovoGiocatore, nascitaGiocatore: nascitaNuovoGiocatore, etàGiocatore: etàNuovoGiocatore, ruoloGiocatore: ruoloNuovoGiocatore)
// REMOVE THIS var rosa = Rosa.testRosa()
rosaArray.append(nuovoGiocatore)
dismiss()
}
}
}
}
}