Finally I made it! Again thanks to Jerry Krinock & Corbin Dunn. I've troubled by this issue for quite some days, and you saved me! =)

I'm not sure whether it's the most proper way to do it, but the code works, I can change title, change background color and change checkbox state by clicking it, and it's pretty simple & easy.

I post the code here in case other guys may have the same trouble some day in the future. And please correct me if anything wrong with my code.

//
//  checkboxHeaderCell.h
//

#import <Cocoa/Cocoa.h>


@interface CheckboxHeaderCell : NSButtonCell {
NSButtonCell *cellCheckBox;
NSColor *bkColor;
}

-(void)setTitle:(NSString *)title;
-(void)setBkColor:(NSColor *)color;
-(BOOL)getState;
-(void)onClick;

@end


//
//  checkboxHeaderCell.m
//

#import "CheckboxHeaderCell.h"


@implementation CheckboxHeaderCell

- (id)init
{
if (self = [super init])
{
 bkColor = nil;

 cellCheckBox = [[ NSButtonCell alloc] init];
 [cellCheckBox setTitle:@""];
 [cellCheckBox setButtonType:NSSwitchButton];
 [cellCheckBox setBordered:YES];
 [cellCheckBox setImagePosition:NSImageLeft];
 [cellCheckBox setAlignment:NSCenterTextAlignment];
 [cellCheckBox setObjectValue:[NSNumber numberWithInt:1]];

 [cellCheckBox setControlSize:NSMiniControlSize];
 [cellCheckBox setFont:[NSFont systemFontOfSize:[NSFont
           smallSystemFontSize]]];
}
return self;
}

- (void)dealloc
{
[cellCheckBox release];
[bkColor release];
[super dealloc];
}

-(void)setTitle:(NSString *)title
{
[cellCheckBox setTitle:title];
}

-(void)setBkColor:(NSColor *)color
{
[color retain];
[bkColor release];
bkColor = color;
}

-(BOOL)getState
{
return [[cellCheckBox objectValue] boolValue];
}

-(void)onClick
{
BOOL state = ![[cellCheckBox objectValue] boolValue];
[cellCheckBox setObjectValue:[NSNumber numberWithBool:state]];
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if (bkColor != nil)
 [cellCheckBox setBackgroundColor:bkColor];

[cellCheckBox drawWithFrame:cellFrame inView:controlView] ;
}

@end

In the delegate of that tableView:

// Clicked on CheckBoxCell
- (void)tableView: (NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
{
CheckboxHeaderCell *headerCell = [tableColumn headerCell];
[headerCell onClick];
}



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to