Question

Hi guys I'm new to Cocoa programming and I am getting always NSOffState whether I'm checking or unchecking an NSButtonCell (Check Box Cell in the UI dragged to a cell in an NSTableView). I have a @property IBOutlet NSButtonCell *mySelection, connected to the respective UI and the following code.

- (void) tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
MediaAComparar *media = [mediasRecesEnStock objectAtIndex:row];
NSString *identifier = [tableColumn identifier];

if ([identifier isEqualToString:@"seleccion"])
{
    if ([mySelection state] == NSOnState)
    {
        [media setValue:object forKey:@"seleccion"];
        NSLog(@"on state");
    }

    if ([mySelection state] == NSOffState)
    {
       [media setValue:object forKey:@"seleccion"];
        NSLog(@"off state");

    }

}

}

I never get the NSOnState to execute, the only NSLog message I get is: off state. Can anyone give me some help? Thanks!!

Was it helpful?

Solution

If you have one outlet ("mySelection") and multiple rows, which row did you think the outlet connects to? (Answer: none of them. You probably hooked it up to the prototype cell, which is never displayed or used directly.)

But no matter, you don't need to check the state before you set it. Assuming your other code is correct, you should be able to do something like:

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    MediaAComparar *medium = [mediasRecesEnStock objectAtIndex:row];

    if ([tableColumn.identifier isEqualToString:@"seleccion"])
        medium.seleccion = object.booleanValue;
}

Less code is better code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top