How to dynamically create sections in a SwiftUI List/ForEach and avoid “Unable to infer complex closure return type”

后端 未结 2 1083
鱼传尺愫
鱼传尺愫 2020-12-09 06:37

I\'m trying to recreate a view that matches the event list in the stock iOS Calendar app. I have the following code which generates a list of events with each event separate

2条回答
  •  我在风中等你
    2020-12-09 07:37

    This is actually two questions.

    In the data part, please upgrade userData.occurrences from [Occurrence] to [[ Occurrence ]] (I called it latestOccurrences, here)

       var self.userData.latestOccurrences = Dictionary(grouping: userData.occurrences) { (occurrence: Occurrence) -> String in
    let dateFormatter = DateFormatter()
    dateFormatter.dateStyle = .medium
    dateFormatter.timeStyle = .none
    return dateFormatter.string(from:  occurrence.start)
     }.values
    

    In the swiftUI, you just reorganize the latest data:

        NavigationView {
        List {
            ForEach(userData.latestOccurrences, id:\.self) { occurrenceSameDate in
                Section(header: Text("\(occurrenceSameDate[0].start, formatter: DateFormatter.init())")) {
                    ForEach(occurrenceSameDate){ occurrence in
                    NavigationLink(
                        destination: OccurrenceDetail(occurrence: occurrence)
                            .environmentObject(self.userData)
                    ) {
                        OccurrenceRow(occurrence: occurrence)
                        }
                    }
                }
            }
        }
        .navigationBarTitle(Text("Events"))
    }.onAppear(perform: populate)
    

提交回复
热议问题