ios - 将 MotionModel 记录分类为基于日期的部分以插入 ProMotion TableScreen 的最有效方法?

标签 ios ruby rubymotion motionmodel

我正在为 iOS 应用程序使用 RubyMotion、ProMotion 和 MotionModel。我们将每天存储多个对日期/时间敏感的条目,并将它们列在一个大的 TableView 中。现在条目出现在一个大列表中:

enter image description here

我想做的是按日期分割条目以提高可读性:

enter image description here

我假设我可以通过循环遍历我的模型条目一次,提取所有唯一日期的数组,然后遍历日期数组并提取匹配的条目来完成这项工作。这会起作用,但我忍不住认为可能有更好的方法来使用我不熟悉的一些 Ruby 魔法来解决这个问题。非常感谢任何建议!

如果阅读本文的人了解 Ruby 或 RubyMotion 但不熟悉 ProMotion,我需要返回的数据格式为以下格式:

[{
  title: "June 12, 2013",
  cells: [{
    title: "10:00am"
  },{
    title: "3:15pm"
  },{
    title: "7:00pm"
  },{
    title: "10:15pm"
  }]
},{
  title: "June 06, 2013",
  cells: [{
    title: "10:00am"
  },{
    title: "3:15pm"
  },{
    title: "7:00pm"
  },{
    title: "10:15pm"
  }]
}]

编辑:

当前按时间倒序生成表格单元格的代码:

[{
  cells: Entries.order{|one, two| two.entry_date <=> one.entry_date}.all.map{ |f|
    {
    title: Time.at(f.entry_date).strftime("%I:%M %p"), #"%m/%d/%Y"
    action: :show_entry,
    arguments: 
      {
        entry: f 
      }
    }
  }
}]

最佳答案

假设 times 有您的时间实例数组:

df=NSDateFormatter.new.tap { |f| f.setDateStyle(NSDateFormatterMediumStyle) }
tf=NSDateFormatter.new.tap { |f| f.setTimeStyle(NSDateFormatterMediumStyle) }
times.inject({}) { |a, t|  (a[df.stringFromDate(t)] ||= []) << 
  tf.stringFromDate(t); a }.map { |k, v| Hash[title: k, cells: v.map { |t| {title: t} } ] }

关于ios - 将 MotionModel 记录分类为基于日期的部分以插入 ProMotion TableScreen 的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17352574/

相关文章:

用于解压缩文件的 iOS native 类或库?

ruby-on-rails - 如何将变量值插入 ERB 模板中的 HTML 标签?

ruby - 运行 pod 命令生成 ruby​​ 错误 : Symbol not found: _ffi_type_double

ios - 错误 ITMS-9000 : "This bundle is invalid. Apple 目前不接受构建的应用程序

ios - 是否可以不基于应用程序名称设置默认CloudKit容器?

iphone - 如何开始在中间 View 中显示启用分页的 UIScrollView?

iOS 9 和 iOS 10 CoreData 同时运行

ruby - 对于不同的平台有不同的 bundler Gemfiles(不使用配置文件中的选项)

macos - RubyMotion 中的指针与 MacRuby 中的指针

ios - Cocoapods 从 1.2.1 更新到 1.4.0 打破了 FontasticIcons