Mr Aleph Mr Aleph - 1 month ago 16
Objective-C Question

NSTAbleView is not displaying contents of the NSMutableArray

In Xcode 4 I created a new Cocoa application called

Tabletest
, on the xib I added a
NSTableView
and control-dragged it to the app's
delegate object
(created automatically when you create the new Cocoa app). I set the table's dataSource and delegate to the app's delegate object called
Tabletest App Delegate
.

On
tabletestAppDelegate.h
and
tabletestAppDelegate.m
I added the (apparently) required

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

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

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


and declared an
NSMutableArray
like
NSMutableArray * myArray;


Then I control-dragged the table to the
.h
and created a property like:

@property (assign) IBOutlet NSTableView *myTable;


On the
.m
file I added the implementation of
numberOfRowsInTableView
and
(id)tableView...


and added:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
myArray = [[NSMutableArray alloc] initWithCapacity:10];
int i = 0;
for(i=0; i<10;i++)
{
[myArray insertObject: [NSString stringWithFormat:@"This is string %d!",i+1] atIndex:i];
}

NSEnumerator * enumerator = [myArray objectEnumerator];
id element;

while((element = [enumerator nextObject]))
{
// Do your thing with the object.
NSLog(@"%@", element);
}

}


The `NSLog show the array gets filled but the info never shows on the table. What am I missing? I am a complete newbie on Cocoa and I have no idea why adding information to a simple table is so complicated.

Thank for the help.

Answer

I'll answer my own question, add

[_myTable reloadData];