开发者

Showing date from two rows in table view, respectively in two text fields, via binding

开发者 https://www.devze.com 2023-01-26 18:08 出处:网络
I have a class named as transaction in which one attribute is transactionDate which is of type NSDate.

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 t开发者_如何转开发ransactions 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


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:

Showing date from two rows in table view, respectively in two text fields, via binding

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.

Showing date from two rows in table view, respectively in two text fields, via binding

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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号