objective-c – 如何填充NSTableView?

前端之家收集整理的这篇文章主要介绍了objective-c – 如何填充NSTableView?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在通过控制器(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

解决方法

How would I:

  • Populate my NSMutableArray which is an array of Person objects

第1步:创建Person对象.

第2步:Add them to the array.

您注释掉的代码就是这样做的,尽管您可能需要单独创建Person,以防您想要配置它(例如,设置其名称).

Should I populate the NSMutableArray in my mainViewDidLoad method of my base class instead?

用户看到你创建它的模型之前有多远并不重要,但从概念上讲,它对我来说有点气味.它与视图没有任何关系,所以我说它属于init.

当然,如果主视图 – 以及它中的每个视图 – 已经加载,你需要tell the table view to reload your data才能显示你对数组所做的任何更改.相反,如果在加载视图之前创建模型,则最初不需要重新加载,因为表视图已经询问过您的模型一次.

猜你在找的cocoa相关文章