2

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);
            */
        }
    }
}
Community
  • 1
  • 1
ruipacheco
  • 15,025
  • 19
  • 82
  • 138

1 Answers1

2

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
  • 60,946
  • 14
  • 140
  • 135
  • 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. – ruipacheco Jan 27 '10 at 11:40
  • When you set your state, are you asking the table to -reloadData (or -reloadDataForRowIndexes:columnIndexes: to be more precise)? – Joshua Nozzi Jan 27 '10 at 12:12
  • 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. – ruipacheco Jan 27 '10 at 12:17
  • 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 Jan 27 '10 at 14:49
  • 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. – ruipacheco Jan 27 '10 at 15:14
  • 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 Jan 27 '10 at 15:40
  • Your comments and an entry (http://www.cocoadev.com/index.pl?ButtonsInTableOrOutlineViews) on Cocoa Dev helped me solve it. Thanks! – ruipacheco Jan 28 '10 at 12:31