Michael Rivers
Michael Rivers

Reputation: 920

View Based NSTableView Not showing data

I have a view-based TableView that I am adding trivial data. However the view is not populating correctly.

 - (int)numberOfRowsInTableView:(NSTableView *)aTableView {

int rows=2;

return rows;
}

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn  row:(int)rowIndex {

// The return value is typed as (id) because it will return a string in most cases.


NSTableCellView *aTableCellView = [aTableView makeViewWithIdentifier:aTableColumn.identifier owner:self];

if ([aTableColumn.identifier isEqualToString:@"FirstName"]) {
    aTableCellView.textField.stringValue = @"Mike";
}

if ([aTableColumn.identifier isEqualToString:@"LastName"]) {
    [aTableCellView.textField setStringValue:@"Bagger"];
}

return aTableCellView;

}

NSImageView

Upvotes: 2

Views: 1332

Answers (2)

Craig Otis
Craig Otis

Reputation: 32104

If you want to use view-based tables (and already have an NSTableCellView subclass defined in Interface Builder), you should instead override and implement the tableView:viewForTableColumn:row: delegate method:

NSTableViewDelegate Protocol Reference

This method should contain the body of your implementation above, populating and returning the NSTableCellView that you've created.

Upvotes: 2

Vikram Singh
Vikram Singh

Reputation: 1816

You should just return the value for the column instead of setting it explicitly. So, instead of doing

aTableCellView.textField.stringValue = @"Mike";

you should just write

return @"Mike"

So, your code should look like below:

if ([aTableColumn.identifier isEqualToString:@"FirstName"]) {
    return @"Mike";
}

if ([aTableColumn.identifier isEqualToString:@"LastName"]) {
    return @"Bagger"];
}

return nil;

Upvotes: -1

Related Questions