Re: Checkboxes in NSTableView
Re: Checkboxes in NSTableView
- Subject: Re: Checkboxes in NSTableView
- From: Scott Herz <email@hidden>
- Date: Tue, 14 Aug 2001 09:29:11 -0700
Your code doesn't seem to store any state. The usual route is to have an
array of dictionaries. Each entry in the array represents a row and each
dictionary key represents a column.
in objectValueForTableColumn, you return the correct value (YES or NO)
depending on the state of your dictionary for that row. The button cell,
which you've set as the data cell will draw itself. There's only one
instance of the cell. It goes stamp stamp stamp for each row. You're
allocating a new button every time a cell needs to draw. Set the data
cell for the column and forget it. (apologies to Ron Popiel)
Then in:
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object
forTableColumn:(NSTableColumn *)tableColumn row:(int)row;
You take the value of the cell and store it in the dictionary for the
indicated row so you can retrieve it in the next
objectValueForTableColumn call. The thing to remember here is you can't
use the cell to store state, because there's only one.
Scott.
On Monday, August 13, 2001, at 11:59 AM, Mark T wrote:
I'm having some problems with using checkboxes(NSSwitchButton) in one
of the columns of a table view. I've followed what directions I could
find on this list and some other places and have successfully gotten
the checkboxes to display. The problem is that they seem unable to hold
a value. I've even set the action for them to set their state to on,
but sometime before the next click, it resets to off. Here is (I think)
all the code pertaining to it:
//In controller.m
- (void)awakeFromNib
{
NSButtonCell *aCell = [[[NSButtonCell alloc] init] autorelease];
[theTable setDataSource:theDataSource];
[theTable setDelegate:self];
[aCell setControlSize: NSRegularControlSize];
[aCell setTitle:@""];
[aCell setButtonType:NSSwitchButton];
[aCell setShowsStateBy:NSContentsCellMask];
[aCell setEnabled:YES];
[aCell setTransparent:NO];
[aCell setState:YES];
[aCell setTarget:self];
[aCell setAction:@selector(checkboxChanged:)];
[[theTable tableColumnWithIdentifier:@""] setDataCell:aCell];
}
- (IBAction)checkboxChanged:(id)sender
{
NSCell * aCell = [[sender tableColumnWithIdentifier:@""]
dataCellForRow:[sender selectedRow]];
NSLog(@"State: %d", [aCell state]);
[aCell setNextState];
NSLog(@"State: %d", [aCell state]);
}
//in datasource.m
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn
row:(int)rowIndex
{
NSButton * aButton = [[NSButton alloc] init];
NSParameterAssert(rowIndex >= 0 && rowIndex < [self
numberOfRowsInTableView:aTableView]);
[aButton setTitle:@""];
[aButton setButtonType:NSSwitchButton];
[aButton setEnabled:YES];
[aButton setTransparent:NO];
[aButton setTarget:self];
[aButton setAction:@selector(checkboxChanged:)];
if([[aTableColumn identifier] isEqualTo:@""])
return aButton;
/* the rest of the code for other columns... */
}
Is this wrong? Is it missing vital parts? both?
Any help you be appreciated,
Mark T.
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev