MTA MTA - 1 month ago 19
Objective-C Question

NSTableView cell text not changed

I am using

NSTableView
in my osx application:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
if (tableView == self.categoryTable) {
return [[[AppAPI shared] categoryArr] count];
}
return 0;
}

- (nullable id)tableView:(NSTableView *)tableView objectValueForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row {
if (tableView == self.categoryTable) {
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];

CategoryItem * cat = [[AppAPI shared] categoryArr][row];

cellView.textField.stringValue = cat.name;

return cellView;
}

return nil;
}


I use this code to insert data to the table, but all i get is rows with this text : enter image description here

The
cellView
is not nil

Any idea why it happens?

Answer

You are using the wrong delegate / data source methods

objectValueForTableColumn:row is supposed to return the object for the row

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    return [[AppAPI shared] categoryArr][row];
}

viewForTableColumn:row: returns the view.

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    return [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
}

PS: How many table views are in your design that you have to check for the right one?

Comments