Binding NSTableColumn to multiple NSPopUpButtonCell's
Binding NSTableColumn to multiple NSPopUpButtonCell's
- Subject: Binding NSTableColumn to multiple NSPopUpButtonCell's
- From: Steven Kramer <email@hidden>
- Date: Tue, 20 Jul 2004 15:25:07 +0200
Hello all,
I have been working on a largish bindings-based application for a while
now. All in all, with help from people on this list (and you didn't
even know it ;-) I've been able to tackle most of the UI without too
much trickery.
Now I find that I'm even able to have a popup in a tablecolumn whose
selection and content change depending on the row. I am not entirely
sure that my solution is the best or even correct though. I am curious
if other people have used similar UI setups with a better solution for
the popup bindings...
The UI shows a table of values and operators. The list of operators
should change depending on the type of the value. Basically, I've
overridden NSTableColumn's dataCellForRow so it will return the right
datacell (out of a grand total of 2). This datacell is programmatically
bound upon creation to a content array suitable for the value's type.
So far so good. The problem lies with the binding to the selected
value/object. Binding the column in IB or in dataCellForRow fails: it
will bind all rows to one cell, not both. I end up with fields that
receive values from the wrong list.
To solve this, I bind 'late', in the tableview's willDisplayCell
method. This seems to work admirably, but I'm not sure if it's robust
enough. If another row needs displaying while editing a row, the
binding might change,for instance. Also, it seems as if I'm doing
NSArrayControllers work there. Also, in the very same application, I
have a similar setup with NSComboBoxCell that 'just works'. This is
probably because the value doesn't need to be in the contentObjects
array.
I have not found any posts on this subject, so I pasted some code below
for people who are interested in doing this.
Regards,
Steven
- (id) dataCellForRow: (int) row
{
CBFilter* filter = [[filtersController arrangedObjects] objectAtIndex:
row];
NSPopUpButtonCell* cell = nil;
if ([[filter type] isEqual: @"date"]) {
static NSCell* dateopscell = nil;
if (dateopscell == nil)
dateopscell = [[[NSPopUpButtonCell alloc] init] retain];
cell = dateopscell;
[cell bind: @"content" toObject: operatorsController withKeyPath:
@"selection.date" options: nil];
[cell bind: @"contentValues" toObject: operatorsController
withKeyPath: @"selection.date.description" options: nil];
}
else {
static NSCell* textopscell = nil;
if (textopscell == nil)
textopscell = [[[NSPopUpButtonCell alloc] init] retain];
cell = textopscell;
[cell bind: @"content" toObject: operatorsController
withKeyPath:@"selection.text" options: nil];
[cell bind: @"contentValues" toObject: operatorsController
withKeyPath: @"selection.text.description" options: nil];
}
[cell setBordered: NO];
return cell;
}
- (void) tableView: (NSTableView*) tableView willDisplayCell: (id) cell
forTableColumn: (NSTableColumn*) tableColumn row: (int) row
{
if ([[tableColumn identifier] isEqual: @"operator"]) {
CBFilter* filter = [[filtersController arrangedObjects]
objectAtIndex: row];
[cell bind: @"selectedValue" toObject: filter withKeyPath: @"value"
options: nil]; /// Yes, I did try an NSObjectController on filter too
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.