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
I just made a test project to see what is going on.
If you put a breakpoint on
applicationDidFinishLaunchingand also onnumberOfRowsInTableViewyou’ll see that thenumberOfRowsInTableViewmethod is called before theapplicationDidFinishLaunchingnotification 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.