hi,
i would suggest first breaking out your todaysLlist
into a dictionary, grouping by league_id
:
let groups = Dictionary(grouping: todaysLlist, by: { $0.league_id })
the groups
variable has the type [Int : [MatchItem]]
, where the keys of the dictionary are the values of league_id
, and the values of the dictionary are arrays of MatchItems sharing the same league_id
.
but you'll want to restructure this data into an array that can be used in a ForEach
to produce sections. the simplest way to do this is to define a computed variable in your View with all of this together:
var groupData: [[MatchItem]] {
let groups = Dictionary(grouping: todaysLlist, by: { $0.league_id })
var returnData = [[MatchItem]]()
for leagueID in groups.keys { // you may want to use groups.keys.sorted(), else the order is unpredictable
returnData.append(groups[leagueID]!)
}
return returnData
}
now you can display the data in a View:
List {
ForEach(groupData) { group in
Section(header: headerFor(group: group)) {
ForEach(group) { matchItem in
// display the view of matchItem
}
}
}
}
the function to get the header for each group of MatchItems is simple: pick any on the MatchItems and read its league_id
, because all of the MatchItems in the group have the same league_id
. something like this:
func headerFor(group: [MatchItem]) -> some View {
Text("league with id \(group[0].league_id)")
}
hope that gives you enough to work with,
DMG
- (slight edits were made above about 15 minutes after the intial posting to correct some naming issues)