views:

76

answers:

1

Hi,

I've a table view to which I add columns dynamically. It must be done this way because I can't predict how many or which columns I will need.

Some columns are checkboxes but I can't click on them when I run my application. The column and checkbox are set to be editable but if I click on the checkbox the check won't get set. Am I missing something?

Update

How I'm (trying) setting the state on the checkbox:

- (void)tableView:(NSTableView *)theTableView 
   setObjectValue:(id)theObject 
   forTableColumn:(NSTableColumn *)theColumn 
              row:(int)rowIndex
{
    if (theTableView == resultsTableView) {

        if ([[theColumn identifier] isEqualToString:CHCheckBoxColumnIdentifier]) {

            NSInteger state = [[theColumn dataCell] state];
            if (state == NSOnState) {
                [[theColumn dataCell] setState:NSOffState];
            } else {
            [[theColumn dataCell] setState:NSOnState];
            }

            /*
            NSLog(@"%@", theObject);
            NSLog(@"%@", theColumn);
            NSLog(@"%i", rowIndex);
            */
        }
    }
}
+1  A: 

Are your columns bound to a controller or are you using the NSTableDataSource protocol? I suspect the latter but you'll need to specify.

Going on my assumption: a click on a checkbox is handled the same way as anything else in the -tableView:setObjectValue:forTableColumn:row: method. Your object will be the state of the button ...

Joshua Nozzi
Doing that, but when I do a setState on the NSButtonCell it doesn't get set, so I assumed that would be the wrong way to go about doing it.
Rui Pacheco
When you set your state, are you asking the table to -reloadData (or -reloadDataForRowIndexes:columnIndexes: to be more precise)?
Joshua Nozzi
Nope. Just set the state and that's it. Should I ask the table to reloadData? Because that would reload everything from the array and I would lose the checkbox I just checked.
Rui Pacheco
Just noticed, that's for 10.6 and my code must run on 10.5.
Rui Pacheco
Just call -reloadData, then - that's the "original" way to refresh a table's contents when it's changed. I'm assuming you're using NSTableDataSource ... you really should read the documentation on this protocol. Essentially, if you change something, you need the table to -reloadData to reflect that change.
Joshua Nozzi
Tried that. The problem is, the checkbox doesn't seem to accept the click (no check ever appears if I click on it) and doesn't listen when I change the state. reloadData doesn't affect anything and it should be counter productive as it rebuilds the table, thus eliminating the checkbox I just clicked.
Rui Pacheco
You're setting the state of the *checkbox*? That's your problem: your checkbox needs to set the state of your model, then when you refresh your table, the checkboxes for each row will reflect that state. I mean no disrespect, but you *really* need to read the NSTableDataSource documentation. At the very least, you should post your (relevant) code so we can leave illustrative comments on it.
Joshua Nozzi
Your comments and an entry (http://www.cocoadev.com/index.pl?ButtonsInTableOrOutlineViews) on Cocoa Dev helped me solve it. Thanks!
Rui Pacheco