开发者

Using distinctUnionOfObjects with an NSArrayController and NSTableView

开发者 https://www.devze.com 2023-03-15 07:04 出处:网络
I\'m having problem filtering an NSArrayController\'s content and binding it to the columns of an NSTableView.

I'm having problem filtering an NSArrayController's content and binding it to the columns of an NSTableView.

In fact, I have a form that I use it to generate bills for our customers. To 开发者_开发知识库do this I have an array called Customer and I can collect each customer's information there.

Now, I want to show all our customers using an NSTableView and of course I need it to be duplicate-free. I know I can use @distinctUnionOfObjects to create an unique array of customers, but it didn't work in Interface Builder's Binding Inspector menu. When I use the Controller Key: arrangedObjects and Model Key Path: @distinctUnionOfObjects.customerName, I receive an Error telling me the Entity is not KVC compliant !!!

2011-06-28 00:22:03.212 FinancingPro[459:903] [<NSManagedObject 0x10053e1a0> valueForUndefinedKey:]: the entity Customer is not key value coding-compliant for the key "@distinctUnionOfObjects".

Maybe distinctUnionOfObjects is not the right way!

How can I have an NSTableView with unique parameters?


When I use the Controller Key: arrangedObjects and Model Key Path: @distinctUnionOfObjects.customerName, I receive an Error telling me the Entity is not KVC compliant !!!

2011-06-28 00:22:03.212 FinancingPro[459:903] [<NSManagedObject 0x10053e1a0> valueForUndefinedKey:]: the entity Customer is not key value coding-compliant for the key "@distinctUnionOfObjects".

You've bound to your model object, not an array controller. arrangedObjects is a property of an array controller, and @distinctUnionOfObjects is a special key only supported by arrays and sets, so you need to bind to your array controller.

Note that your @distinctUnionOfObjects filter will only apply to that column, not other columns. Thus, you are only filtering the customers' names, not the customers themselves. This is very probably wrong; you'll still be able to have multiple customers with the same name in the model, and when you do, the table view will either flip out (because you've given it multiple columns of differing lengths) or show what is effectively garbage (wrong names because they're shifted up relative to the other columns).

Furthermore, let's say you have a table view with only a name column, such as a source list, and you apply this filtering to it. Then, when you have duplicate customers, the app will only show one of them, and it will be a random one. If the user deletes that customer, the other (or one of the others) will show up; this will appear as if the deletion failed (“I deleted John Smith, but John Smith still appears in the list!”).

If you intend to disallow multiple customers with the same name, do that when customers are entered or changed using Key-Value Validation, and remove this output restriction in the table view. If, somehow, your user gets multiple users in their database with the same name, you don't want your application to go insane.

You need to prevent duplicates in input, not output.

0

精彩评论

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

关注公众号