PDA

View Full Version : NSMatrix selectedCells: Only Returns Single NSButtonCell in Array?




Kelmon
Aug 13, 2006, 10:17 AM
OK, I thought I had a solution to a problem in my application nailed but now I am confused. I have an instance of NSMatrix in my interface that contains a single column of 6 NSButtonCell objects, each set to type "Check Box". The idea is that the NSMatrix "relates" these check boxes together and allows me to iterate through them in order to get the title of each selected Check Box using the following code:


// Get the NSButtonCell objects for the selected Sub Doc types and add their
// titles to an array of strings
NSMutableArray *subDocsTitles = [[NSMutableArray alloc] init];
NSArray *subDocButtonCells = [subDocTypesMatrix selectedCells];

int count = 0;
while (count < [subDocButtonCells count]) {
[subDocsTitles addObject:[[subDocButtonCells objectAtIndex:count] title]];
count++;
}


The problem at the moment is that I can select as many of the check boxes as I like but the NSArray returned by [subDocTypesMatrix selectedCells] only contains a single instance of NSButtonCell (the last one selected). Any ideas why the other selected cells aren't being returned in the array?



Kelmon
Aug 13, 2006, 03:20 PM
Well, my conclusion at this time is that this is a bug in Cocoa. Given that there already exists the -(id)selectedCell: method that returns a pointer to the last selected cell, it makes no sense that -(NSArray *)selectedCells: (emphasis on the selectedCells plural) returns only a single cell in the array. I've worked around this using the following code:


// Get the NSButtonCell objects for the Sub Doc types and add their
// titles to an array of strings if they are selected
NSMutableArray *subDocsTitles = [[NSMutableArray alloc] init];
NSArray *subDocButtonCells = [subDocTypesMatrix cells];

int count = 0;
while (count < [subDocButtonCells count]) {
NSLog(@"Selected Button Title is %@ and its value is %d",
[[subDocButtonCells objectAtIndex:count] title],
[[subDocButtonCells objectAtIndex:count] intValue]);
if ([[subDocButtonCells objectAtIndex:count] intValue] == 1) {
[subDocsTitles addObject:[[subDocButtonCells objectAtIndex:count] title]];
}
count++;
}