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];
}
}
NSDictionar开发者_运维技巧y *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.
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
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];
}
精彩评论