NSTableView中设置selectedRow的高亮颜色

有两种方法:第一种是继承NSCell:

                      第二种是继承NSTableView。

在tableView中,使用

  1. [myView setUsesAlternatingRowBackgroundColors:NO];//取消行与行之间蓝白交替显示的背景  
[myView setUsesAlternatingRowBackgroundColors:NO];//取消行与行之间蓝白交替显示的背景

继承NSCell:

  1. @interface subNSTableCell : NSCell  
  2. {  
  3.     NSColor*                _cellBKColor;  
  4.     NSColor*                _cellFontColor;  
  5.     NSAttributedString*                     _cellAttributedString;  
  6. }  
  7.   
  8. - (void)setSelectionBKColor:(NSColor*)cellColor;  
  9. - (void)setSelectionFontColor:(NSColor*)cellFontColor;  
  10. - (NSAttributedString*)getCellAttributes;  
  11.   
  12. @end  
@interface subNSTableCell : NSCell
{
	NSColor*				_cellBKColor;
	NSColor*				_cellFontColor;
	NSAttributedString*                     _cellAttributedString;
}

- (void)setSelectionBKColor:(NSColor*)cellColor;
- (void)setSelectionFontColor:(NSColor*)cellFontColor;
- (NSAttributedString*)getCellAttributes;

@end

设置color和属性:

  1. - (NSAttributedString*)getCellAttributes  
  2. {  
  3.     NSDictionary*  _attributes = [NSDictionary dictionaryWithObjectsAndKeys:_cellFontColor,NSForegroundColorAttributeName,nil];  
  4.     NSString* _cellString = [self stringValue];  
  5.       
  6.     _cellAttributedString = [[[NSAttributedString alloc]  
  7.                               initWithString:_cellString attributes:_attributes] autorelease];  
  8.       
  9.     return _cellAttributedString;  
  10. }  
- (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:

  1. - (NSColor*)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView  
  2. {  
  3.     NSRect newRect = NSMakeRect(cellFrame.origin.x - 1, cellFrame.origin.y, cellFrame.size.width + 5, cellFrame.size.height);  
  4.     if (_cellBKColor)  
  5.     {  
  6.         [_cellBKColor set];  
  7.         NSRectFill(newRect);  
  8.     }  
  9.       
  10.     [self setAttributedStringValue:[self getCellAttributes]];  
  11.       
  12.     return nil;  
  13. }  
- (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。

  1. subNSTableCommonCell*   tTableCell = nil;  
  2.       
  3.     tTableCell = [[subNSTableCommonCell alloc] initTextCell:@""];  
  4.       
  5.     [tTableCell setSelectionBKColor:[NSColor lightGrayColor]];  
  6.     [tTableCell setSelectionFontColor:[NSColor redColor]];  
  7.       
  8.     [[[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可以自行设置。

  1. - (id)_highlightColorForCell:(id)cell   
  2. {  
  3.       
  4.     if([self selectionHighlightStyle] == 1)   
  5.     {  
  6.         return nil;  
  7.     }   
  8.     else   
  9.     {  
  10.         return _highlightBKColor;  
  11.     }  
  12. }  
- (id)_highlightColorForCell:(id)cell 
{
	
	if([self selectionHighlightStyle] == 1) 
	{
		return nil;
	} 
	else 
	{
		return _highlightBKColor;
	}
}
另外,设置选中状态字体颜色等可以放在以下方法中执行。

  1. - (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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值