开发者

How do I create a Cocoa GUI to edit an array of NSDate objects?

In one app I am working on I need to let the user edit a list of dates. I have those NSDate objects in an array and bound that to a NSArrayController. I bound a NSTableColumn (the only column in a table view) to that array controller using the key path arrangedObjects.self. This works fine - I get all the dates displayed in the table just the way I want it and I can add and开发者_开发知识库 remove dates.

The trouble is I can not edit the dates in the table. If I try I get an exception telling me, that NSDate is not key-value-coding compliant to the key self, which of course is true, there is no setSelf: method.

I have two ideas how I could solve this:

  1. have my array not store NSDate directly but put it in an NSMutableDictionary or some other object first.
  2. not allow the user to edit the date directly but have him remove the wrong one and re-add the corrected date.

I don’t really like both solutions. I don’t like the first because I have to put stuff in my model just to get the GUI right. And the second is not very user-friendly.

So does anyone have an idea if there is a third and even better way to solve this?

Update: Temporary solution

For now I am using the table view’s data source to deal with this instead of using bindings. But I’d still like to see if there is a better solution using bindings for that problem, since I don’t really want to write a data source every time this (or a similar problem) turns up. It happens not just with NSDate but all other value type classes provided by cocoa, even if they are mutable (like NSMutableString).


Updated

It seems that, since NSDate has no properties you can set (as with NSNumber), in order to "edit a date" you have to replace the "old" one with a newly created one. The date must be a property of some other object or part of a container like an NSDictionary for NSDateFormatter and Cocoa Bindings to do the right thing.

Apologies for the earlier noise - I always seem to learn something new when I try something I never tried before. :-)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜