views:

371

answers:

2

Hey everyone,

I'm working on learning Objective-C/Coaoa, but I've seem to have gotten a bit stuck with getting the NSTableView object to work for me. I've followed all the directions, but for some reason I still get this error:

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

Here's my source, tell me what you see is wrong here, I'm about to tear my face off.

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

I'm running OS X 10.6.1 if that makes any difference. Thanks in advance.

+4  A: 

For one thing, the data source methods now deal with NSIntegers, not ints.

More relevantly, if your deployment target is Mac OS X 10.6 or later, then you need to declare your data source's class as conforming to the NSTableViewDataSource formal protocol in your class's @interface. (This protocol and many others are new in 10.6; previously, they were informal protocols.)

Peter Hosey
+3  A: 

Try changing the declaration of the @interface to the following:

@interface RobotManager : NSObject <NSTableViewDataSource> {

This will tell the compiler that the RobotManager class follows the NSTableViewDataSource protocol.

Edit:

In addition, it's probably likely that the RobotList is not being initialized before the two methods of NSTableViewDataSource are being called. In otherwords, awakeFromNib is not being called.

Unless there is an explicit call to the awakeFromNib from some caller, the RobotList won't be initialized, so rather than populating the RobotList in that method, try populating it when the RobotManager is first instantiated.

coobird
This got rid of the warning, but data still isn't showing up in the NSTableView.
Vestonian
Nakedsteve: You haven't created an array and put it in the `RobotList` variable. That variable holds `nil`. So you're sending your `addObject:` messages to `nil`, which does nothing, and asking for the count of `nil`, which returns 0.
Peter Hosey
Yep, that did it. I added `RobotList = [[NSMutableArray alloc] init];` and now its working great. Thanks everyone!
Vestonian