0
votes

I have a NSTableView where I wish to display a list of info. Currently the viewForTableColumn method delegate never runs, but numberOfRowsInTableView does.

I have the NSTableViewDelegate and NSTableViewDataSource set in the ViewController head. And I set the tableview delegate and datasource to self. Does somebody know why it wouldn't run? I've added a screenshot and code below.

ViewController.h

@interface ViewController : NSViewController <NSTableViewDelegate, NSTableViewDataSource>

@property (strong) IBOutlet NSTableView *tableView;
@property (strong, nonatomic) NSMutableArray<App *> *installedApps;

@end

ViewController.m

 - (void)viewDidLoad {
    [super viewDidLoad];

    _installedApps = [[NSMutableArray alloc] init];

    _tableView.dataSource = self;
    _tableView.delegate = self;

    // Other stuff that populates the array
}

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
    return _installedApps.count;
}

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSTableCellView *result = [tableView makeViewWithIdentifier:@"appCell" owner:self];
    result.textField.stringValue = @"Hello world";

    return result;
}

The view is in a container view, I have the 'appCell' identifier set to the Table Cell View.

Screenshot of my storyboard

3
BTW: You should use accessors. Use self.installedApps instead of _installedApps because the later might produce problems with KVO and subclassing. - clemens
Do you call [self.tableView reloadData] when fill your array? - Kevinosaurio
Is _tableView nil? - Willeke
@Willeke Nope not nil. - John
@clemens Thanks, I'll start doing that from now on. - John

3 Answers

0
votes

The array _installedApps is empty and numberOfRowsInTableView: returns 0. Thus, tableView:viewForTableColumn: is not called because there is no row to show. No rows also means no columns.

You should also ensure that you have configured your table view as view based in attributed inspector of the table view.

0
votes

I can't see it in the screenshots, but...is the highlighted row of the view hierarchy (Table Cell View) the one with the appCell identifier?

[minutes pass...]

Oops; sorry. I see you've noted that above.

The reason I ask is that I made a new project from your code, changing the array type from App to NSString, added a one-column table view to the storyboard, linked it to the code, added a couple enties to the array in -viewDidLoad, and -- once I put the appCell identifier in the right place (duh) -- it all worked fine.

0
votes

This is super strange, but I had the very same issue, everything connected correctly, number of rows being called, but not viewForTableColumn... In the end the following Answer proved to be the solution:

https://stackoverflow.com/a/13091436/3963806

Basically, I had setup the tableview straight out of the Object library, no layout constraints etc... Once I added layout constraints, the method started to be called... Super strange as I could see and click on the "rows" but they weren't populated correctly... I think it's down to clipping as mention in the linked answer.