NSTAbleView does not display NSMutableArray content

In Xcode 4, I created a new Cocoa application called Tabletest , on xib I added an NSTableView and dragged it into the delegate object application (created automatically when I created the new Cocoa application). I set up the dataSource table and delegated an application delegation object named Tabletest App Delegate .

In tabletestAppDelegate.h and tabletestAppDelegate.m I added (apparently) the 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 by NSMutableArray as NSMutableArray * myArray;
Then I control - drag the table to .h and create a property like:

 @property (assign) IBOutlet NSTableView *myTable; 

In the .m file, I added an 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); } } 

"NSLog shows that the array is being filled, but the information is never displayed in the table. What am I missing? I'm a complete Cocoa newbie and I have no idea why adding information to a simple table is so difficult.

Thanks for the help.

0
source share
3 answers

I will answer my question, add

 [_myTable reloadData]; 
0
source

applicationDidFinishLaunching fires when the view is already loaded. At this time, your array is empty. Calling reloadData on a table should fix this problem.

+2
source

There is another aspect to displaying contens: If you add a new element to your array programmatically, that is, dynamically at any time in your application (and not at the initialization stage in applicationDidFinishLaunching).

In this case, create an IBOutlet for your array controller and call the reordering functions

  // ... adding items to myArray [myArray insertObject: yadayada ...]; // then let the controller rearrange the objects [arrayCtrl rearrangeObjects]; 

This behaves like an β€œupdate” table with a new item.

0
source

Source: https://habr.com/ru/post/897396/


All Articles