开发者

How to refresh an NSTableVIew when you populate the NSMutableArray (bind) associated with it

开发者 https://www.devze.com 2023-01-22 06:51 出处:网络
I tried to bind my NSArraycontroller to an NSMutableArray; the array holds objects of type \"iData\" (it开发者_开发百科\'s a custom class). The class further contains some NSMutableString variables wh

I tried to bind my NSArraycontroller to an NSMutableArray; the array holds objects of type "iData" (it开发者_开发百科's a custom class). The class further contains some NSMutableString variables which are set as keys for KVC. Then I bound the my NSTableColumn to the NSArrayController and set key model paths of every column respectively.

When I try to populate the array, the GUI does not show anything. What did I forget?


So it's likely that you solved this long ago, but in case someone else stumbles across this question...

I am populating the array via NSMutableArray, but I am not sure how can I populate the array via NSArrayController, since I don't have the instance in my Controller.m class. Please tell me how can I resolve this issue.

It is possible that you were doing something like

[myData addObject:someObject];

However, your NSArrayController will not learn of this change to the NSMutableArray instance because addObject is not KVC compliant. You need to inform any object that is observing that your NSMutableArray instance has changed. There are at least two ways to do this. Assuming that your NSMutableArray instance property is named "myData", then you can do something like the following:

[self.willChangeValueForKey:@"myData"];
[myData addObject:someObject];
[self.didChangeValueForKey:@"myData"];

or

NSMutableArray *bindingsCompliantArray = [self mutableArrayValueForKey:@"myData"];
[bindingsCompliantArray addObject:someObject];

Another SO answer (linked) has a good explanation on what mutableArrayValueForKey actually does, but I recommend reading the Apple developer docs on key-value coding and key-value observation to help understand it.

I have dragged the NSController instance in my mainmenu.nib tray. Do i need to declare an IBOutLet NSArrayController in my Controller.h file and then connect it with the NSArrayController instance in the tray ?

You need a NSArrayController instance in your nib file, but you do not need an IBOutlet in your interface for the situation that you've described here. The NSArrayController should be bound to the key of your NSMutableArray (myData in my example) and it sounds like you already have your table columns bound correctly.


Although Stephen's answer is probably "the way to go", I think the OP's original question "How do I insert/delete/manage my NSMutableArray using the NSArrayController, deserves a simpler and more direct answer:

NSArrayController provides a full and rich set of methods and even IBActions to fulfill almost anything you want on the managed NSMutableArray, with all the niceties of handling things "through filters", through selection, and "keeping sorting rules" e.g. insert a new item so that it is inserted according to the current sort-descriptions.

Here's an excerpt from these methods (open NSArrayController.h for the full set) and remember that the 'content' is your NSMutableArray, while 'arrangedObjects' is an array provided by the NSArrayController that applies filtering and sorting to the content, "on its way" to the display in the NSTableView.

- (BOOL)addSelectedObjects:(NSArray *)objects;
- (BOOL)removeSelectedObjects:(NSArray *)objects;

- (IBAction)add:(nullable id)sender;    // overridden to add a new object to the content objects and to the arranged objects
- (IBAction)remove:(nullable id)sender;    // overridden to remove the selected objects
- (IBAction)insert:(nullable id)sender;

- (void)addObject:(id)object;    // overridden to add to the content objects and to the arranged objects if all filters currently applied are matched
- (void)addObjects:(NSArray *)objects;
- (void)insertObject:(id)object atArrangedObjectIndex:(NSUInteger)index;    // inserts into the content objects and the arranged objects (as specified by index in the arranged objects) - will raise an exception if the object does not match all filters currently applied
- (void)insertObjects:(NSArray *)objects atArrangedObjectIndexes:(NSIndexSet *)indexes;
- (void)removeObjectAtArrangedObjectIndex:(NSUInteger)index;    // removes from the content objects and the arranged objects (as specified by index in the arranged objects)
- (void)removeObjectsAtArrangedObjectIndexes:(NSIndexSet *)indexes;
- (void)removeObject:(id)object;    // removes from the content objects and the arranged objects (if currently contained)
- (void)removeObjects:(NSArray *)objects;

All this, of course, is for direct programmatic control over the content, and not "automagically" via Cocoa-Bindings.

0

精彩评论

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

关注公众号