Displaying NSPopUpButtonCells in NSTableView
Displaying NSPopUpButtonCells in NSTableView
- Subject: Displaying NSPopUpButtonCells in NSTableView
- From: James Maxwell <email@hidden>
- Date: Thu, 20 May 2010 13:14:57 -0700
I thought I'd change the topic on this, since the content really has changed.
I have an table for setting up MIDI instruments with a name, port, and channel. I've got the initial display of options for the table row displaying correctly, and when I make changes to the name, port, and channel, the data object is updated correctly. The only thing left is that the popups in columns 2 and 3 are not holding the selected value. I understand that this is probably because I'm reloading the menu in tableView:objectValueForTableColumn:row, but I'm not sure how to get around it.
Here's the code I've got so far. I'm not using an NSArrayController, because that approach was driving me absolutely mental...
I've copied all the code (except imports) to be thorough. Logging "Edited MIDIInstrument" at the end shows that the instrument is set up correctly, with the desire name, port, and channel. It's just that the popups always display the 2nd item.
thanks,
J.
@implementation MIDI_Instrument_Controller
@synthesize instrumentLibrary;
@synthesize selectedInstrument;
- (id) init
{
self = [super init];
if(self)
{
NSMutableArray* inst = [[NSMutableArray alloc] init];
[self setInstrumentLibrary:inst];
[inst release];
}
return self;
}
- (IBAction) addInstrumentToLibrary: (id)sender
{
MIDIInstrument* newInst = [[MIDIInstrument alloc] init];
[[self instrumentLibrary] addObject:newInst];
[self setSelectedInstrument:newInst];
[newInst release];
[midiInstrumentsTable reloadData];
NSIndexSet* idxs = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(([[self instrumentLibrary] count] - 1), 1)];
[midiInstrumentsTable selectRowIndexes:idxs byExtendingSelection:NO];
[midiInstrumentsTable editColumn:0 row:([[self instrumentLibrary] count] - 1) withEvent:nil select:YES];
}
- (IBAction) removeInstrumentFromLibrary: (id)sender
{
if([self selectedInstrument] != nil)
{
[[self instrumentLibrary] removeObject:[self selectedInstrument]];
[midiInstrumentsTable reloadData];
}
}
- (void) tableViewSelectionDidChange: (NSNotification*)notification
{
int row = [midiInstrumentsTable selectedRow];
if(row >= 0)
[self setSelectedInstrument:[[self instrumentLibrary] objectAtIndex:row]];
}
- (NSInteger) numberOfRowsInTableView:(NSTableView*)tv
{
if(tv == midiInstrumentsTable)
return [[self instrumentLibrary] count];
return 0;
}
- (id) tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tc row:(NSInteger)row
{
NSLog(@"Updating instruments table...");
if(tv == midiInstrumentsTable)
{
id inst = [[self instrumentLibrary] objectAtIndex:row];
if(inst == nil)
return nil;
if(tc == midiInstNamesColumn)
{
// give the name for the associated MIDIInstrument
[tc setEditable:YES];
return [inst MIDI_Inst_Name];
}
else if(tc == midiPortsColumn)
{
// list the ports for the associated MIDIInstrument
[tc setEditable:YES];
NSPopUpButtonCell* ports = [tc dataCell];
[ports removeAllItems];
int i;
for(i=0;i < [[inst MIDI_Inst_Ports] count];i++)
[ports addItemWithTitle:[[inst MIDI_Inst_Ports] objectAtIndex:i]];
return ports;
}
else if(tc == midiChannelsColumn)
{
// list the channels for the associated MIDIInstrument (just 16 numbers)
[tc setEditable:YES];
NSPopUpButtonCell* channels = [tc dataCell];
[channels removeAllItems];
int i;
for(i=0;i < 16;i++)
[channels addItemWithTitle:[NSString stringWithFormat:@"%i", i+1]];
return channels;
}
}
return nil;
}
- (void) tableView:(NSTableView *)tv setObjectValue:(id)v forTableColumn:(NSTableColumn *)tc row:(NSInteger)row
{
NSLog(@"Updating instruments...");
if (tv == midiInstrumentsTable)
{
MIDIInstrument* inst = [[self instrumentLibrary] objectAtIndex:row];
if(inst == nil)
return;
if (tc == midiInstNamesColumn)
{
// set the given name
[inst setMIDI_Inst_Name:v];
NSLog(@"MIDI Instrument edited: %@", v);
}
else if(tc == midiPortsColumn)
{
// set the selected port
[inst setMIDI_Inst_Port:[[inst MIDI_Inst_Ports] objectAtIndex:[v intValue]]];
NSLog(@"MIDI Port changed: %@", [inst MIDI_Inst_Port]);
}
else if(tc == midiChannelsColumn)
{
// set the selected channel
[inst setMIDI_Inst_Channel:[NSNumber numberWithInt:[v intValue] + 1]];
}
NSLog(@"Edited MIDIInstrument: %@", inst);
}
}
@end
James B Maxwell
Composer/Doctoral Student
School for the Contemporary Arts (SCA)
School for Interactive Arts + Technology (SIAT)
Simon Fraser University
email@hidden
email@hidden
_______________________________________________
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