资料:https://stackoverflow.com/questions/19421314/custom-nstableheadercell-for-clear-background-of-nstableview
#import "MTDownloadTableHeaderCell.h"
@implementation MTDownloadTableHeaderCell
-(instancetype)initTextCell:(NSString *)string {
if (self = [super initTextCell:string]) {
}
return self;
}
-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
[super drawWithFrame:cellFrame inView:controlView];
[iColor(0xff, 0xff, 0xff, 1.0) setFill];
NSRectFill(cellFrame);
[self drawInteriorWithFrame:cellFrame inView:controlView];
}
-(void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
CGRect titleRect = [self titleRectForBounds:cellFrame];
titleRect.origin.x += 15;
titleRect.origin.y += 5;
titleRect.size.width -= 15;
[self.attributedStringValue drawInRect:titleRect];
}
@end
//设置自定义头视图
-(void)setCustomTableHeaderView {
[self.tableView setBackgroundColor:[NSColor clearColor]];
[[self.tableView tableColumns] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSString *columnTitle = [[obj headerCell] stringValue];
MTDownloadTableHeaderCell *myCell = [[MTDownloadTableHeaderCell alloc] initTextCell:columnTitle];
[obj setHeaderCell:myCell];
}];
}
网友评论