我目前通过控制器 (MVC设计模式) 来填充 NSTableView,我在控制器的 init
方法中初始化了一个 NSMutableArray 的一项。
我应该如何:
- 填充我的 NSMutableArray,它是 Person 对象的数组
- 我应该在我的基类的
mainViewDidLoad
方法中填充 NSMutableArray 吗? 我没有找到任何关于这方面的示例或资源。
模型 (Person.m)
#import "Person.h"
@implementation Person
@synthesize name;
@synthesize gender;
- (id)init
{
self = [super init];
if (self) {
name = @"Bob";
gender = @"Unknown";
}
return self;
}
- (void)dealloc
{
self.name = nil;
self.gender = nil;
[super dealloc];
}
@end
控制器(PersonController.m)
#import "PersonController.h"
#import "Person.h"
@implementation PersonController
- (id)init
{
self = [super init];
if (self) {
PersonList = [[NSMutableArray alloc] init];
// [personList addObject:[[Person alloc] init]];
//
// [personTable reloadData];
}
return self;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [personList count];
}
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
Person *person = [personList objectAtIndex:row];
NSString *identifier = [tableColumn identifier];
return [person valueForKey:identifier];
}
- (void)dealloc
{
[super dealloc];
}
@end
基础文件(Main.h):
#import "Main.h"
@implementation Main
- (void)mainViewDidLoad
{
}
@end