Binding an NSTableView to an NSMutableArray
I'm learning Cocoa, and I've got a problem: I would like to bind the content of an NSMutableArray to an NSTableView, with bindings. I read many documentations about them, but I can't manage to make them work (nothing is displayed in my table).
Here are the facts:
I created a simple model, called MTMTask
which contains 2 properties, priority
and text
:
MTMTask.h
@interface MTMTask : NSObject {
NSString *priority;
NSString *text;
}
@property(copy) NSString* priority;
@property(copy) NSString* text;
- (id) initWithPriority :(NSString*)newPriority andText:(NSString*)newText;
@end
MTMTask.m
#import "MTMTask.h"
@implementation MTMTask
@synthesize text, priority;
- (id) initWithPriority:(NSString *)newPriority andText:(NSString *)newText {
if (self = [super init]) {
priority = newPriority;
text = newText;
return self;
}
return nil;
}
@end
Then I've created the MTMTaskController :
MTMTaskController.h
#imp开发者_JAVA技巧ort <Cocoa/Cocoa.h>
#import "MTMTask.h"
@interface MTMTaskController : NSObject {
NSMutableArray *_tasksList;
}
- (NSMutableArray *) tasksList;
@end
MTMTaskController.m
#import "MTMTaskController.h"
@implementation MTMTaskController
- (void) awakeFromNib
{
MTMTask *task1 = [[MTMTask alloc] initWithPriority:@"high" andText:@"Feed the hungry cat"];
MTMTask *task2 = [[MTMTask alloc] initWithPriority:@"low" andText:@"Visit my family"];
_tasksList = [[NSMutableArray alloc] initWithObjects:task1, task2, nil];
}
- (NSMutableArray*) tasksList
{
return _tasksList;
}
@end
And finally I edited the MainMenu.xib: I added a NSObject
and set its class to MTMTaskController
. Then I added an NSArrayController, called TasksListController
, with its content outlet bound to MTMTaskController.tasksList
. I also set its mode to Class
and class name MTMTask
. I bound the value
outlet of two columns of an NSTableView
to TasksListController
text and priority.
But when I run the program, well, it's not really a success: nothing shows up in the table.
Have you got an idea concerning my problem? I think I'm missing something but I can't figure what.
Thanks in advance!
When you are allocating objects for the controller in awake from nib, you create the objects, add them to an array and then set that array as the task list.
The thing about bindings is that you need to be aware of KVO (Key value observing) which is the mechanism by which bound objects know that the things they have bound to have changed.
In the awake from nib method you have just set the array directly which doesn't invoke KVO.
I have created an example Xcode project (Xcode 3.1) which you can download from here. This creates a property for the task list and within the awakeFromNib method I am assigning the array using property syntax, which takes care of KVO for you:
- (void)awakeFromNib {
Task *task1 = [[Task alloc] initWithPriority:@"high" andText:@"Feed the cat"];
Task *task2 = [[Task alloc] initWithPriority:@"low" andText:@"Visit my familiy"];
self.taskArray = [[NSMutableArray alloc] initWithObjects:task1, task2, nil];
}
Alternatively, you could sandwich the assignment in willChangeValueForKey:
and didChangeValueForKey:
messages, but I'll leave that as an excercise for you.
精彩评论