I have an NSTableView which I use in conjunction with a data source. I have manually created one column to represent row numbers, and I create 5 more in the awakeFromNib method. However when I call a method from a different class (when a toolbar item is clicked), and I try to get the selectedColumn, it always returns 0. Here's my code.
- (void)awakeFromNib {
tableDict = [[NSMutableDictionary alloc] init];
int i;
for (i = 1; i <= 5; i ++) {
NSTableColumn *newColumn;
newColumn = [[NSTableColumn alloc] initWithIdentifier:[NSString stringWithFormat:@"%i", i]];
[[newColumn headerCell] setStringValue:@"Field"];
[[newColumn headerCell] setAlignment:NSCenterTextAlignment];
[newColumn setEditable:YES];
[newColumn setWidth:100.0];
[myTableView addTableColumn:newColumn];
[newColumn release];
NSMutableArray *colArray = [[NSMutableArray alloc] init];
int j;
for (j = 1; j <= 1024; j ++) {
[colArray addObject:@""];
}
[tableDict setObject:colArray forKey:[NSString stringWithFormat:@"%i", i]];
[colArray release];
}
[tableDict retain];
[myTableView retain];
NSLog(@"%i\n", [myTableView selectedColumn]);
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
NSLog(@"numberOfRows: %@\n", [[tableDict objectForKey:@"1"] count]);
return 1024;
}
- (void)fillView:(NSString *)theContents {
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
[[tableDict objectForKey:[aTableColumn identifier]] replaceObjectAtIndex:rowIndex withObject:anObject];
[theWindow setDocumentEdited:YES];
}
- (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)col row:(int)row
{
if ([[col identifier] isEqualTo: @"rownum"]) {
return [NSString stringWithFormat:@"%i", row + 1];
}
else {
return [[tableDict objectForKey:[col identifier]] objectAtIndex:row];
}
}
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectTableColumn:(NSTableColumn *)aTableColumn {
//NSLog(@"%@\n", [aTableColumn identifier]);
if([[aTableColumn identifier] isEqualToString:@"rownum"]) {
return NO;
}
return YES;
}
- (NSString *)tableView:(NSTableView *)aTableView toolTipForCell:(NSCell *)aCell rect:(NSRectPointer)rect tableColumn:(NSTableColumn *)aTableColumn row:(int)row mouseLocation:(NSPoint)mouseLocation {
if ([[aTableColumn identifier] isEqualTo: @"rownum"]) {
return @"";
}
else {
return [[tableDict objectForKey:[aTableColumn identifier]] objectAtIndex:row];
}
}
- (void)insertColumn {
NSLog(@"%i\n", [myTableView selectedColumn]);
}