有两种方法:第一种是继承NSCell:
第二种是继承NSTableView。
在tableView中,使用
- [myView setUsesAlternatingRowBackgroundColors:NO];//取消行与行之间蓝白交替显示的背景
[myView setUsesAlternatingRowBackgroundColors:NO];//取消行与行之间蓝白交替显示的背景
继承NSCell:
- @interface subNSTableCell : NSCell
- {
- NSColor* _cellBKColor;
- NSColor* _cellFontColor;
- NSAttributedString* _cellAttributedString;
- }
- - (void)setSelectionBKColor:(NSColor*)cellColor;
- - (void)setSelectionFontColor:(NSColor*)cellFontColor;
- - (NSAttributedString*)getCellAttributes;
- @end
@interface subNSTableCell : NSCell
{
NSColor* _cellBKColor;
NSColor* _cellFontColor;
NSAttributedString* _cellAttributedString;
}
- (void)setSelectionBKColor:(NSColor*)cellColor;
- (void)setSelectionFontColor:(NSColor*)cellFontColor;
- (NSAttributedString*)getCellAttributes;
@end
设置color和属性:
- - (NSAttributedString*)getCellAttributes
- {
- NSDictionary* _attributes = [NSDictionary dictionaryWithObjectsAndKeys:_cellFontColor,NSForegroundColorAttributeName,nil];
- NSString* _cellString = [self stringValue];
- _cellAttributedString = [[[NSAttributedString alloc]
- initWithString:_cellString attributes:_attributes] autorelease];
- return _cellAttributedString;
- }
- (NSAttributedString*)getCellAttributes
{
NSDictionary* _attributes = [NSDictionary dictionaryWithObjectsAndKeys:_cellFontColor,NSForegroundColorAttributeName,nil];
NSString* _cellString = [self stringValue];
_cellAttributedString = [[[NSAttributedString alloc]
initWithString:_cellString attributes:_attributes] autorelease];
return _cellAttributedString;
}
然后重写方法highlightColorWithFrame:inView:
- - (NSColor*)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
- {
- NSRect newRect = NSMakeRect(cellFrame.origin.x - 1, cellFrame.origin.y, cellFrame.size.width + 5, cellFrame.size.height);
- if (_cellBKColor)
- {
- [_cellBKColor set];
- NSRectFill(newRect);
- }
- [self setAttributedStringValue:[self getCellAttributes]];
- return nil;
- }
- (NSColor*)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect newRect = NSMakeRect(cellFrame.origin.x - 1, cellFrame.origin.y, cellFrame.size.width + 5, cellFrame.size.height);
if (_cellBKColor)
{
[_cellBKColor set];
NSRectFill(newRect);
}
[self setAttributedStringValue:[self getCellAttributes]];
return nil;
}
最后,在tableView中使用子类化的cell。
- subNSTableCommonCell* tTableCell = nil;
- tTableCell = [[subNSTableCommonCell alloc] initTextCell:@""];
- [tTableCell setSelectionBKColor:[NSColor lightGrayColor]];
- [tTableCell setSelectionFontColor:[NSColor redColor]];
- [[[mytableView tableColumns] objectAtIndex:0] setDataCell:tTableCell];
subNSTableCommonCell* tTableCell = nil;
tTableCell = [[subNSTableCommonCell alloc] initTextCell:@""];
[tTableCell setSelectionBKColor:[NSColor lightGrayColor]];
[tTableCell setSelectionFontColor:[NSColor redColor]];
[[[mytableView tableColumns] objectAtIndex:0] setDataCell:tTableCell];
这样继承的cell,在处于选中状态时,能够按照我的设置背景显示成lightGrayColor,cell中的字体为redColor。
但是当选中的一行含有多列时,cell与cell之间的空隙会被系统的那个高亮颜色——蓝色给占据。即使只有一行时,在cell的边框能很明显的看见蓝色。
然后在网上查找到的资源,继承NSTableView,直接改变处于选中状态的系统设置的蓝色。
继承NSTableView:
将以下方法加入到子类化的tableView中可以将tableView中处于选中状态的行按照_highlightBKColor的颜色来显示。_highlightBKColor可以自行设置。
- - (id)_highlightColorForCell:(id)cell
- {
- if([self selectionHighlightStyle] == 1)
- {
- return nil;
- }
- else
- {
- return _highlightBKColor;
- }
- }
- (id)_highlightColorForCell:(id)cell
{
if([self selectionHighlightStyle] == 1)
{
return nil;
}
else
{
return _highlightBKColor;
}
}
另外,设置选中状态字体颜色等可以放在以下方法中执行。
- - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex