SWIFTUI中的分区分组

SWIFTUI中的分区分组,swift,swiftui,Swift,Swiftui,我从firebase中的数据库获取数据,数据模型如下: var label: String var date: Double var description: String var categoryId: String var ownerId: String var patientId: String var motconsuId: String 接收数据时,我希望在categoryId字段旁边的一个部分中收集数据。但我不能这样做,因为某些原因,它们没有分组和复制 我在视图中显示的代码: L

我从firebase中的数据库获取数据,数据模型如下:

var label: String
var date: Double
var description: String
var categoryId: String
var ownerId: String
var patientId: String
var motconsuId: String
接收数据时,我希望在categoryId字段旁边的一个部分中收集数据。但我不能这样做,因为某些原因,它们没有分组和复制

我在视图中显示的代码:

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)
    }
格言:

    @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:[(键:字符串,值:[MotconsuModel])]{

返回字典(分组:vm.motconsus,按:{$0.categoryId})。排序(按:{$0.key<$1.key})

}

您应该给出正确的属性,而不是使用与
motconsu
不同的属性,必须连接到
类别的数组

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)
}
列表{

ForEach(self.motconsoloadservice.??,id:\.categoryId){//您链接到的图像没有太多说明,您是否可以添加一些示例数据作为文本,但更重要的是,您应该在问题中添加motconsoloadservice.motconsu的定义
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)
}