1
我正在閱讀由Tim Isted編寫的「Beginning Mac Programming」一書,我被一個與NSTableView打交道的程序卡住了。 我有一個從NSDocument繼承的可可應用程序。該應用程序包含一個NSTableView。我已將表視圖的數據源設置爲文件所有者。執行後,我得到*非法的NSTableView數據源()。必須實現numberOfRowsInTableView:和的tableView:objectValueForTableColumn:行:NSTableView非法數據源
MyDocument.h的接口是
#import <Cocoa/Cocoa.h>
@interface MyDocument : NSDocument {
IBOutlet NSTextField *newItemTextField;
IBOutlet NSTableView *shoppingListTableView;
NSMutableArray *shoppingListArray;
}
-(IBAction)addNewItemToShoppingList:(id)sender;
-(void)dealloc;
-(NSInteger)numberOfRowsInTableView;
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex;
@end
而實現
#import "MyDocument.h"
@implementation MyDocument
- (id)init
{
self = [super init];
if (self) {
shoppingListArray = [[NSMutableArray alloc] initWithObjects:@"pane",@"latte",@"pasta", nil];
}
return self;
}
-(IBAction)addNewItemToShoppingList:(id)sender{
NSString *item = [newItemTextField stringValue];
[shoppingListArray addObject:item];
[shoppingListTableView reloadData];
NSLog(@"allocated");
}
-(void)dealloc{
[shoppingListArray release];
[super dealloc];
}
-(NSInteger)numberOfRowsInTableView{
return [shoppingListArray count];
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex{
return [shoppingListArray objectAtIndex:rowIndex];
}
我在做什麼錯???非常感謝。
哦,那真的很愚蠢的錯過。謝謝。 – tagomago 2012-02-27 22:22:57