NSTableView 中的自定义日期格式

发布于 2025-01-04 00:01:28 字数 409 浏览 2 评论 0原文

我的应用程序兼顾历史事件。有些事件的日期可能是公元前数千年。显然,此类事件的日期不是很准确,最终会出现“大约公元前 20,000 年”或“公元前 50,000 年之前”之类的术语。

为了解决这个问题,我创建了一个自定义视图,允许用户在输入事件时构建自定义日期格式。然后将此格式与事件日期一起存储为 NSString 的实例,以供以后显示。对于我在每个视图中显示一个事件的视图来说,这一切都非常有效。

但是,当我想在 NSTableView 中显示多个事件时,我陷入了困境。我向 NSTableView 中的表格单元格添加了一个日期格式化程序,但似乎无法将我存储的日期格式绑定到单元格中的日期格式化程序。

我怎样才能实现这个目标?请记住,表中每一行的格式字符串可能不同......

My app juggles historic events. Some event dates can be thousands of years BC. Obviously the dates for such events are not very accurate and end up in terms like "About 20,000 BC" or "Before 50,000 BC".

To tackle this, I have created a custom view which allows the user to build a custom date format when entering an event. This format is then stored - for later display purposes - with the date of the event as an instance of NSString. This all works quite well for views where I show one event per view.

However, when I want to display several events in an NSTableView, I'm stuck. I added a date formatter to the table cell in my NSTableView but there appears to be no way to bind my stored date format to the date formatter in the cell.

How can I achieve this? Bear in mind that the format string can be different for each row in the table...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2025-01-11 00:01:28

我有类似的需求,还没有想出一个很好的解决方案,但我也没有花很多时间去尝试。

由于它需要逐行变化,因此想到的是使格式化程序成为模型对象的属性,然后在表列上有一个“元格式化程序”来询问 objectValue 并返回结果将模型的格式化程序应用于模型的数据值。 (如果这有任何意义......)

编辑:伪代码

在您的自定义 NSFormatter 子类(“元格式化程序”)中,您将实现类似以下内容:

- (NSString *)stringForObjectValue:(id)object {
    return [[object customFormatter] stringForObjectValue:[object date]];
}

实际上,非绑定元格式化程序充当内部表示的每行自定义格式化程序。让它与 Core Data 一起工作,好吧,你就得靠你自己了。 :-)

I have a similar requirement and haven't come up with a great solution yet, but I also haven't spent a lot of time trying.

What comes to mind, since it needs to vary on a row-by-row basis, is to make the formatter a property of your model object, then have a "meta formatter" on your table column that interrogates the objectValue and returns the result of applying the model's formatter to the model's data value. (If that makes any sense at all...)

Edit: pseudo-code

In your custom NSFormatter subclass (the "meta formatter") you would implement something like:

- (NSString *)stringForObjectValue:(id)object {
    return [[object customFormatter] stringForObjectValue:[object date]];
}

In effect, the non-bound meta formatter acts as a proxy for the internally-represented per-row custom formatter. Making it work with Core Data, well, you're on your own there. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文