我正在学习Objective-C / Coaoa,但我似乎已经因为让NSTableView对象适合我而陷入困境。我遵循了所有指示,但由于某种原因,我仍然会收到此错误:

Class 'RobotManager' does not implement the 'NSTableViewDataSource' protocol

这是我的来源,告诉我你看到的是错的,我要把脸撕下来。

RobotManager.h

@interface RobotManager : NSObject {
 // Interface vars
 IBOutlet NSWindow *MainWindow;
 IBOutlet NSTableView *RobotTable;
 NSMutableArray *RobotList;
}

- (int) numberOfRowsInTableView: (NSTableView*) tableView;
- (id) tableView:(NSTableView *) tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex;
@end

RobotManager.m

#import "RobotManager.h"

@implementation RobotManager

-(void) awakeFromNib {
 // Generate some dummy vals
 [RobotList addObject:@"Hello"];
 [RobotList addObject:@"World"];
 [RobotTable setDataSource:self]; // This is where I'm getting the protocol warning
 [RobotTable reloadData];
}

-(int) numberOfRowsInTableView: (NSTableView *) tableView {
 return [RobotList count];
}

-(id) tableView:(NSTableView *) tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex {
 return [RobotList objectAtIndex:rowIndex];
}

@end

我正在运行OS X 10.6.1,如果这有任何区别的话。提前谢谢。

有帮助吗?

解决方案

尝试将 @interface 的声明更改为以下内容:

@interface RobotManager : NSObject <NSTableViewDataSource> {

这将告诉编译器 RobotManager 类遵循 NSTableViewDataSource 协议。

修改

此外,在调用 NSTableViewDataSource 的两个方法之前,很可能没有初始化 RobotList 。换句话说, awakeFromNib 未被调用。

除非某些调用者显式调用 awakeFromNib ,否则 RobotList 将不会被初始化,因此不会填充 RobotList 在该方法中,尝试在首次实例化 RobotManager 时填充它。

其他提示

首先,数据源方法现在处理 NSInteger ,而不是 int

更相关的是,如果您的部署目标是Mac OS X 10.6或更高版本,那么您需要将数据源的类声明为符合您班级的 @interface NSTableViewDataSource 正式协议代码>。 (该协议和许多其他协议在10.6中是新的;以前,它们是非正式协议。)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top