I’m making a “simple” to-do-list project and running into a few bumps in the road. The problem is that my NSTableView is NOT showing the data from the NSMutableArray “myArray” and I don’t know why that is. Can someone point out my mistake?
/*
IBOutlet NSTextField *textField;
IBOutlet NSTabView *tableView;
IBOutlet NSButton *button;
NSMutableArray *myArray;
*/
#import "AppController.h"
@implementation AppController
-(IBAction)addNewItem:(id)sender
{
NSString *myString = [textField stringValue];
NSLog(@"my stirng is %@",myString);
myArray = [[NSMutableArray alloc] initWithCapacity:100];
[myArray addObject:myString];
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [myArray count];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
return [myArray objectAtIndex:rowIndex];
}
-(id)init
{
[super init];
[tableView setDataSource:self];
[tableView setDelegate:self];
NSLog(@"init");
return self;
}
@end
The problem is that in your
-initmethod, your tableView variable will still be nil. When a nib file is loaded, it first creates and initializes all the objects declared in the nib and only then sets up the connections (outlets, bindings, actions) between them. Any initialization code of yours that depends on the various actions, outlets or bindings being set up needs to be placed in the-(void)awakeFromNibmethod not the-initmethod. You can also set the dataSource and delegate outlets of your table view right in Interface Builder, just by control-dragging from the tableView to your AppController in the main nib window, and choosing each outlet in turn.Also, whenever you change your underlying data, you need to call
[tableView reloadData]to get the tableView to reflect those changes. In you case, you’d need to call it in your -addNewItem: method. So, your code should look something like this: