2

Is there no "simple" way to bind an NSArray that contains NSDictionaries with unknown keys to a NSTableView?

What is the best approach to solve this "common" problem?

Coming from c#/asp.net it seems to be a really painful operation.

edit:

To clarify what the app is about: its a simple queryeditor that displays the result of the query in a tableview.

I have tried to follow this example: http://www.cocoabuilder.com/archive/cocoa/150245-dynamic-columns-in-nstableview.html

I use an object that implements the NSTableViewDataSource protocol.

When the user issues the first query, the result is displayed correctly. But the second query shows something strange: the columns are not removed correctly and are added to the existing ones, but not all.

In the method, that builds the table i use something like this:

    NSArray *columns = [_resultTableView tableColumns];

    if(columns && [columns count] > 0)
    {
        for( int i=0; i < [columns count]; i++)
        {
            NSTableColumn *col = [columns objectAtIndex:i];
            NSLog(@"removing column: %@", [col identifier]);
            [_resultTableView removeTableColumn:col];
        }
    }


    NSDictionary *dict = [_resultTableDataSource.data objectAtIndex:0];

    NSArray *keys = [[dict keyEnumerator] allObjects];

    for( int i=0; i < [keys count]; i++)
    {
        NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier:[keys objectAtIndex:i]];
        [column setEditable:NO];
        [[column headerCell] setStringValue:[keys objectAtIndex:i]];
        [_resultTableView addTableColumn:column];
    }

    [_resultTableView setDataSource:_resultTableDataSource];

    [_resultTableView reloadData];

_resultTableDataSource.data is a NSMutableArray with NSMutableDictionary's as records.

2
  • As far as i understand now, i have to implement a NSTableViewDataSource and build the Columns myself, is that right?
    – ovm
    Jun 18, 2011 at 14:39
  • I bet that approach is a how-not-to-example, right?
    – ovm
    Jun 18, 2011 at 17:32

2 Answers 2

0

Take a look at NSArrayController and configure the columns in Interface Builder. Then it's as easy as 1-2-3 ;)

Start here and read on in the NSArrayController description: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/CntrlContent.html

2
  • Thank you. But i do not now the columns in advance.
    – ovm
    Jun 18, 2011 at 17:19
  • i have added some notes to the original question for clarification
    – ovm
    Jun 18, 2011 at 17:34
0

Ok simple enough.

Since i iterate over the columns by reference i have to use 0 as index, since after the removal of the first column, the column count is already minus 1.

Changing the loop to the following solves the problem:

    for( int i=0; i < [columns count]; i++)
    {
        NSTableColumn *col = [columns objectAtIndex:0];
        NSLog(@"removing column: %@", [col identifier]);
        [_resultTableView removeTableColumn:col];
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.