First of all you should not get the index of the column, as the columns can be dragged and hence its index can be changed. However you can do as:
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
if ([tableView tableColumns][0] == tableColumn) {
return [self.array[row] firstName];
}
else if ([tableView tableColumns][1] == tableColumn) {
return [self.array[row] lastName];
}
}
The other way around is to check with the table header cell title. Using this you can decide what value to fill in the column. Something like: (But here you need to set the column header manually)
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
NSCell *headerCell = [tableColumn headerCell];
if ([[headerCell title] isEqualToString:@"First Name"]) {
return [self.array[row] firstName];
}
else if ([[headerCell title] isEqualToString:@"Last Name"]) {
return [self.array[row] lastName];
}
return nil;
}
Also you can opt for Cocoa-Binding, here no need to use identifiers!!!
Edit:
As you don't have class, infact you are dealing with basic C-array. But the delegate returns id
so you need to typecast it to some Obj-C object. In following case I use NSString
. See the screen shot :