通过绑定分别在两个文本字段中显示表视图中两行的日期

发布于 2024-10-03 18:54:20 字数 326 浏览 0 评论 0原文

我有一个名为 transaction 的类,其中一个属性是 transactionDate,其类型为 NSDate。

我正在使用 NSArrayController 在表视图中显示事务列表。

我的要求是——

我想在第一行显示日期 标记为“从”和日期的文本字段 在标记为的文本字段的最后一行中 “到”。

我的问题是-

我可以通过 IB 中的绑定来实现此目的吗? 如果是的话怎么办?

谢谢,

米拉杰

I have a class named as transaction in which one attribute is transactionDate which is of type NSDate.

I am using NSArrayController to display a list of transactions in a table view.

My requirement is-

I want to show date in first row in a
text field labeled as "From" and date
in last row in a text field labeled as
"To".

My questions is-

Can I achieve this via binding in IB?
If yes then how?

Thanks,

Miraaj

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

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

发布评论

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

评论(2

蓝天 2024-10-10 18:54:20

是的,您可以执行此操作(或类似的操作),请参阅 @min 和 @max 数组运算符

以下是如何执行此操作的一个非常简单的版本:

  1. 打开 Interface Builder - 创建一个新应用程序
  2. 添加一个 NSArrayController,将其设置为自动准备内容。
  3. 添加一个表格,标记两列 "name""age"
  4. 添加一个按钮(标记为 "+")和两个标签(change name 为 maxval, minval)
  5. 按如下所示连接 NSArrayController 绑定:

 alt text

这是正在运行的应用程序(仅在界面生成器“模拟”模式下)。要使用它,请单击“+”,然后单击表格的顶行,并在第一列中输入姓名,在第二列中输入年龄。然后再对几个人重复一遍。

alt text

这一切看起来有点神秘,但已经足够了。为了清楚起见,我在我的版本中添加了“最大年龄”和“最小年龄”标签。

Yes, you can do this (or something like it), see the @min and @max array operators.

Here's how to do a very simple version of this:

  1. Open Interface Builder - create a new application
  2. Add an NSArrayController, set it to automatically prepare content.
  3. Add a Table, label the two columns "name" and "age"
  4. Add a Button (labelled "+") and two Labels (change name to maxval, minval)
  5. Wire up the NSArrayController bindings as follows:

alt text

Here's the application running (just in interface builder "simulate" mode). To use it, click + then click on the top row of the table and put a name in the first column and an age in the second. Then repeat for a few more people.

alt text

This all looks a bit cryptic, but it is sufficient. I added the labels "Max Age" and "Min Age" for clarity in my version.

岁月如刀 2024-10-10 18:54:20

不幸的是,这并不是 Bindings 特别擅长的事情。你可以实现它,但它会非常hacky。

NSTableViewDataSource协议仍然是向表提供数据的完全相关且有效的方式。在这种情况下(您不只是向表提供一组简单的统一数据),数据源协议是解决问题的唯一明智的方法。这样您就可以完全控制表格显示的内容。

唯一的“困难”部分是,如果您使用 Core Data,您的数据源类将需要观察托管对象上下文的更改并重新加载数据(使用 -reloadData 刷新整个表或使用 -reloadDataForRowIndexes:columnIndexes :挑选要刷新的行)。

Unfortunately this is not something Bindings is particularly good at. You could achieve it but it would be very hacky.

The NSTableViewDataSource protocol is still a perfectly relevant and valid way to provide data to a table. In cases like these (where you're not just presenting a straight-vanilla set of uniform data to a table) the data source protocol is the only sane way to solve the problem. This way you're in complete control of what the table displays.

The only "hard" part is that, if you're using Core Data, your data source class will need to observe the Managed Object Context for changes and reload the data (either -reloadData to refresh the whole table or use -reloadDataForRowIndexes:columnIndexes: to cherry pick the rows you want to refresh).

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