【问题标题】:Make a grouped list from Realm requestResults从 Realm requestResults 创建一个分组列表
【发布时间】:2020-06-28 04:02:48
【问题描述】:

从 Realm 查询和排序后:

    // fetch all Items sorted by Type
    let results = realm.objects(Paper.self).sorted(byKeyPath: "Type", ascending: false)
    
    var paperCollatedByType: [String : [Paper]] {
        Dictionary(grouping: results, by: { $0.type })
    }
    
    // pulls out the keys from the dictionary made above, sorted
    var uniqueType: [String] {
        paperCollatedByDate.map({ $0.key }).sorted()
    }

这是我制作列表的来源:

["kyasutoYellowSeparator": [Paper {
    type = kyasutoYellowSeparator;
    width = 111;
    rollLength = 600;
    quantity = 1;
    importDate = 2020-06-28 12:11:19 +0000;
    totalLength = 600;
    isSlitted = 1;
}, Paper {
    type = kyasutoYellowSeparator;
    width = 12;
    rollLength = 600;
    quantity = 1;
    importDate = 2020-06-28 03:18:22 +0000;
    totalLength = 600;
    isSlitted = 0;
}]]

我正在考虑创建一个按类型“kyasutoYellowSeparator”分组的列表:

List {
         ForEach(paperCollatedByDate, id: \.self) { (section: [Paper]) in
                Section(header: Text(section[0].type)) {
                      ForEach(section, id: \.self) { paper in
                          Text(paper.type)
                      }
               }
          }//end of list

但它完全失败了,请帮助我制作列表。感谢您花时间阅读我的问题。

【问题讨论】:

    标签: swift swiftui realm


    【解决方案1】:

    这里是固定变体(使用 Xcode 11.4 在复制的简化模型上测试)

    List {
        ForEach(paperCollatedByDate.keys.sorted(), id: \.self) { section in
            Section(header: Text(self.paperCollatedByDate[section]!.first?.type ?? "")) {
                ForEach(self.paperCollatedByDate[section]!, id: \.self) { paper in
                    Text(paper.type)
                }
            }
        }
    }
    

    【讨论】:

    • 那么按 importDate 分组怎么样?
    • 一个问题 - 一个答案,你知道 :) ...顺便说一句 - 我假设你可以找到很多关于分组的帖子,只需搜索。
    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2014-03-10
    • 2020-10-15
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多