【问题标题】:Grouping of sections in SWIFTUISWIFTUI 中的部分分组
【发布时间】:2021-06-14 11:47:59
【问题描述】:

我正在从我的 firebase 数据库中获取数据,数据模型是这样的:

var label: String
var date: Double
var description: String
var categoryId: String
var ownerId: String
var patientId: String
var motconsuId: String

在接收数据时,我想通过 categoryId 字段将它们收集在一个部分中。但我做不到,由于某种原因,它们没有分组和重复。

The result I am getting

我显示视图的代码:

List {
        
        ForEach(self.motconsuLoadService.motconsu, id:\.categoryId) {
            (category) in
            
            Section(header: Text(category.categoryId)) {
                
                ForEach(self.motconsuLoadService.motconsu, id:\.motconsuId) {
                    (motconsu) in
                    MotconsuCard(motconsu: motconsu)
                }
            }
        }
    }.listStyle(GroupedListStyle())
    .onAppear {
        self.motconsuLoadService.loadPatientsMotconsu(patientId: settings.defaultPatient)
    }

Motconsu卡:

    @ObservedObject var patientServices = PatientService()
var motconsu: MotconsuModel

var body: some View {
    
    HStack {
        VStack(alignment: .leading) {
            Text(motconsu.label)
                .font(.system(size: 17))
            Text("\(motconsu.date)")
                .foregroundColor(.gray)
                .font(.system(size: 15))
                .padding(.top, 2)
        }
        Spacer()
        Image(systemName: "chevron.down")
    }
}

我做错了什么,我的错误是什么?

编辑: 更容易,关键是类别:

var groupByCategory: [(key: String, value: [MotconsuModel])] {

return Dictionary(grouping: vm.motconsus, by: { $0.categoryId }).sorted(by: { $0.key < $1.key } )

}

【问题讨论】:

  • 您链接到的图像并没有说太多,您可以添加一些示例数据作为文本,但更重要的是您应该在问题中添加 motconsuLoadService.motconsu 的定义

标签: swift swiftui


【解决方案1】:

你应该给出正确的属性而不是 ??? 使用不同于 motconsu 的东西,??? 必须连接到 category 的数组 p>

List {
    
    ForEach(self.motconsuLoadService.???, id:\.categoryId) {    // <<: Here 
        (category) in
        
        Section(header: Text(category.categoryId)) {
            
            ForEach(self.motconsuLoadService.motconsu, id:\.motconsuId) {
                (motconsu) in
                MotconsuCard(motconsu: motconsu)
            }
        }
    }
}.listStyle(GroupedListStyle())
.onAppear {
    self.motconsuLoadService.loadPatientsMotconsu(patientId: settings.defaultPatient)
}

【讨论】:

    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多