我刚刚开始使用XCode (v3.2.2)和Interface,遇到了一个问题。
以下是我所做的:
我创建了一个类作为NSTableView的数据源:
@interface TimeObjectsDS : NSControl {
IBOutlet NSTableView * idTableView;
NSMutableArray * timeObjects;
}
@property (assign) NSMutableArray * timeObjects;
@property (assign) NSTableView * idTableView;
- (id) init;
- (void) dealloc;
- (void) addTimeObject: (TimeObj *)timeObject;
// NSTableViewDataSource Protocol functions
- (int)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView
objectValueForTableColumn:(NSTableColumn *)tableColumn row:
(int)row;
@implementation TimeObjectsDS
@synthesize timeObjects;
@synthesize idTableView;
-(id) init {
if (self = [super init]) {
self.timeObjects = [[NSMutableArray alloc] init];
TimeObj *timeObject = [[TimeObj alloc] init];
[timeObject setProjectId:11];
[timeObject setDescription:@"Heja"];
[timeObject setRegDate:@"20100331"];
[timeObject setTimeSum:20.0];
[timeObjects addObject:timeObject];
[timeObject release];
[idTableView reloadData];
}
return self;
}
- (void) dealloc {
[idTableView release];
[timeObjects release];
[super dealloc];
}
// Functions
- (void) addTimeObject: (TimeObj *)timeObject {
[self.timeObjects addObject:timeObject];
[idTableView reloadData];
}
// NSTableViewDataSource Protocol functions
- (int) numberOfRowsInTableView:(NSTableView *)tableView {
return [self.timeObjects count];
}
- (id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row {
return [[timeObjects objectAtIndex:row] description];
}
@end然后,我将视图中的NSTableView绑定到这个数据源,如下所示:
big.png
我还将视图NSTableView绑定到界面生成器中的Controller idTableView变量,如上所示
在init函数中,我向可变数组添加了一个元素。当我运行应用程序时,这将在NSTableView中正确显示。但是,当我向数组中添加另一个元素(其类型与init相同)并试图在视图上调用idTableView reloadData时,什么都不会发生。实际上,Controller idTableView是空的。当使用NSLog(@"idTableView:%@,idTableView打印变量时,我得到"idTableView:(null)“
我想不出怎么解决这个问题。我能做些什么来解决这个问题吗?
发布于 2010-04-29 12:32:07
如果控制器中的表视图出口为null,则没有在Interface中连接它。上面的屏幕截图显示了与TimeObjectsDS的连接,但这并不意味着什么--这是您正在调用reloadData的实例吗?例如,您可能拥有这个类的多个实例。
那只是一种可能性。如果没有更多的代码,就不可能列出更多的代码。
顺便说一句,在MVC中,将模型对象直接连接到视图被认为是一件坏事。您可能只是使用了错误的术语。
https://stackoverflow.com/questions/2737189
复制相似问题