我想使用 NSArrayController 来填充 NSTableview,但我无法找出确切的过程。
一种方法是通过 KVC,使用 NSArrayController 来填充 NSTableView。
示例代码:
测试 AppDelegate.h
#import <Cocoa/Cocoa.h>
@interface TestAppDelegate : NSObject <NSApplicationDelegate>
{
IBOutlet NSArrayController *arrayController;
IBOutlet NSTableView *theTable;
}
@property (assign) IBOutlet NSArrayController *arrayController;
@property (assign) IBOutlet NSTableView *theTable;
- (void) populateTable;
@end
测试AppDelegate.m
#import "TestAppDelegate.h"
@implementation TestAppDelegate
@synthesize arrayController;
@synthesize theTable;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Populate the table once with the data below
[self populateTable];
}
- (void) populateTable
{
NSMutableDictionary *value = [[NSMutableDictionary alloc] init];
// Add some values to the dictionary
// which match up to the NSTableView bindings
[value setObject:[NSNumber numberWithInt:0] forKey:@"id"];
[value setObject:[NSString stringWithFormat:@"test"] forKey:@"name"];
[arrayController addObject:value];
[value release];
[theTable reloadData];
}
@end
现在在界面生成器中进行绑定:
- 创建一个 NSArrayController 并将其连接到 arrayController
- 将 NSTableView 连接到 theTable;
- 选择 NSTableView 并将 TestAppDelegate 设置为其数据源和委托
- 对于表中的每一列
- 将其值绑定到数组控制器
- Set the 控制器按键 to 排列对象
- Set the 模型关键路径上面的每个键(例如id or name)
运行时,现在应该有一个数据行。 (这是未经测试的代码,但应该给出总体思路)
有关这些绑定的更多帮助请查看这个例子 http://www.blackdogfoundry.com/blog/binding-nsmutablearray-to-nsarraycontroller/.
这也是一个很好的例子 http://daemonconstruction.blogspot.com/2011/11/simple-nstableview-application-with.html带有如何创建填充 NSTableView 的图片。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)