Skip navigation.
 
mlRe: NSTableView - Programmatically creating Table Columns and populating
FROM : I. Savant
DATE : Mon Jul 31 14:24:06 2006

Zef:

  I created a test project with your code since I couldn't see 
anything "wrong" with it (though your approach to whatever problem 
you're solving looks ... not quite right either. :-)).

  I added an action to respond to a button click to log the selected 
column (basically, I just went ahead and called your -insertColumn 
method, since all it does is logs the selected column). It worked 
just fine.

  I'm thinking you probably don't have your myTableView outlet 
connected to the table view in your nib. When I disconnected the 
myTableView outlet from the NSTableView in the nib, I always got "0", 
no matter which column was selected. *Always verify your outlets!*

  Hope this helps.

--
I.S.


On Jul 29, 2006, at 1:03 PM, Zef RosnBrick wrote:

>
> On Jul 29, 2006, at 8:19 AM, I. Savant wrote:
>

>>
>>  Post. Your. Code.
>>
>>  Telling us your code is broken but not offering the code itself 
>> does absolutely no good. We can't help you if we don't know what's 
>> wrong.
>>
>> --
>> I.S.
>>
>>
>> On Jul 29, 2006, at 1:34 AM, Zef RosnBrick wrote:
>>

>>> I finally got my table view working, but now if wont respond to 
>>> [myTableView selectedColumn] or selectedRow - it always returns 
>>> 0. It also doesn't return anything for number of table columns.

>>

>
>
> - (void)awakeFromNib {
>     tableDict = [[NSMutableDictionary alloc] init];
>     int i;
>     for (i = 1; i <= 64; 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 <= 1028; j ++) {
>             [colArray addObject:@""];
>         }
>         [tableDict setObject:colArray forKey:[NSString 
> stringWithFormat:@"%i", i]];
>         [colArray release];
>     }
> }
> //I use awakeFromNib to add all the table columns I want, and to 
> create the NSDictionary I use as a data source
>
> - (int)numberOfRowsInTableView:(NSTableView *)aTableView {
>     return 1028;
> }
>
> - (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
> {
>     //I have a manually added column to display row numbers - is there 
> a better way?
>    if ([[col identifier] isEqualTo: @"rownum"]) {
>        return [NSString stringWithFormat:@"%i", row + 1];
>    }
>     else {
>         return [[tableDict objectForKey:[col identifier]] 
> objectAtIndex:row];
>     }
>    return nil;
> }
>
> - (void)insertColumn {
>     NSLog(@"%i\n", [myTableView selectedColumn]);
> }
>
> Here is where my code breaks. The method insertColumn is called by 
> a class I'm using to control a toolbar for the window when one of 
> the toolbar items is clicked, however, it always logs 0, no matter 
> which column is selected.
>
> Zef

Related mailsAuthorDate
No related mails found.