Ryan
Ryan

Reputation: 787

numberOfRowsInTableView is not being called

I'm setting up a view-based NSTableView, however the method numberOfRowsInTableView is not being called. When I check whether it is being called, the log returns nil.

I have set up my AppDelegate to conform to the datasource and the delegate protocols and have connected these through IB to the AppDelegate. My array that will feed the table is initialised when app finishes loading.

My code is as follows:

Interface

#import <Cocoa/Cocoa.h>

@interface RLTAppDelegate : NSObject <NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSTableView *tableView;
@property (copy) NSArray *nameArray;

@end

Implementation

#import "RLTAppDelegate.h"

@implementation RLTAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  self.nameArray = [[NSArray alloc] initWithObjects:@"Ryan", nil];
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {

  return [self.nameArray count];
  NSLog(@"%lu", self.nameArray.count);
}

- (NSView *)tableView:(NSTableView *)tableView
   viewForTableColumn:(NSTableColumn *)tableColumn
              row:(NSInteger)row {

NSTextField *result = [tableView makeViewWithIdentifier:@"name" owner:self];
result.stringValue = [self.nameArray objectAtIndex:row];
return result;
}

@end

Upvotes: 1

Views: 1821

Answers (1)

Abizern
Abizern

Reputation: 150565

I just made a test project to see what is going on.

If you put a breakpoint on applicationDidFinishLaunching and also on numberOfRowsInTableView you'll see that the numberOfRowsInTableView method is called before the applicationDidFinishLaunching notification is received.

So you're asking for the array before it has been populated.

Normally, you'd have a separate class where this happens and you set up the array when that class in initialised, so this doesn't happen.

Upvotes: 2

Related Questions