NSSegmentedCell and strange selected segment behavior
NSSegmentedCell and strange selected segment behavior
- Subject: NSSegmentedCell and strange selected segment behavior
- From: Brice Martin <email@hidden>
- Date: Fri, 21 Aug 2009 20:50:03 +0200
Hi there,
I have some trouble setuping a NSSegmentedCell within a NSTableView.
I actually succeeded adding the NSSegmentedCell itself (it appears at
runtime) but setting and getting the selected segment have a strange
behavior.
1- Whatever segment index I might set during the call to
setSelected:forSegment: of NSSegmentedCell, it's always the first segment
which is selected.
2- When I click on a segment at runtime, the control doesn't switch to the
selected segment (this last one highlights itself when the mouse button is
down, but the focus stays on the first segment when the mouse button is up
again)
I should also tell you I'm building the NSSegmentCell instance dynamically
at runtime within the NSTableView tableView:dataCellForTableColumn:row:
delegate method implementation:
- (NSCell*)tableView:(NSTableView *)tableView
dataCellForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSCell *cell = nil;
// build widget and set default value
if ([@"SettingValue" isEqualToString:[tableColumn identifier]]) {
// get selected Setting
Setting *setting = [[settingsArrayController arrangedObjects]
objectAtIndex:row];
if (setting != nil) {
if ([@"text" isEqualToString:[setting widget]]) {
cell = [[[NSTextFieldCell alloc] init] autorelease];
} else if ([@"combo" isEqualToString:[setting widget]]) {
cell = [[[NSComboBoxCell alloc] init] autorelease];
[(NSComboBoxCell*)cell setControlSize:NSMiniControlSize];
// TODO: Finish this
} else if ([@"segmented" isEqualToString:[setting widget]]) {
cell = [[[NSSegmentedCell alloc] init] autorelease];
NSSegmentedCell *scell = (NSSegmentedCell*)cell;
NSArray *segments = [[setting parameters] componentsSeparatedByString:@
","];
[scell setControlSize:NSSmallControlSize];
[scell setTrackingMode:NSSegmentSwitchTrackingSelectOne];
[scell setTarget:self];
[scell setAction:@selector(segmentClicked:)];
[scell setSegmentCount:[segments count]];
int scount = 0;
for (NSString* segment in segments) {
[scell setLabel:segment forSegment:scount];
if ([segment isEqualToString:[setting value]]) {
NSLog(@"Selected segment: %d", scount);
[scell setSelected:YES forSegment:scount];
}
scount++;
}
} else {
// TODO: Display an error in the settings TableView
}
} else {
// TODO: Display an error in the settings TableView
}
[cell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
// disable readonly cells
[cell setEnabled:[[NSNumber numberWithBool:NO] isEqualToNumber:[setting
readOnly]]];
}
return cell;
}
And here is my action handler implementation:
/*
* Selection of a segmented cell
*/
- (IBAction)segmentClicked:(id)sender {
// retrieve selected segment
NSSegmentedCell *cell = [[sender tableColumnWithIdentifier:@"CategoryName"]
dataCellForRow:[sender selectedRow]];
int selectedSegment = [cell selectedSegment];
NSLog(@"Selected segment: %d", selectedSegment);
Setting *setting = [[settingsArrayController arrangedObjects]
objectAtIndex:[sender selectedRow]];
NSArray *parameters = [[setting parameters] componentsSeparatedByString:@
","];
[setting setValue:[parameters objectAtIndex:selectedSegment]];
NSLog(@"New setting value: %@", [parameters
objectAtIndex:selectedSegment]);
}
I'm sure I've done something wrong. So any help is welcome because I'm stuck
on this thing this days now >_>
Huge thanks to the list.
Brice
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden