NSTableView中设置selectedRow的高亮颜色

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

                        第二种是继承NSTableView。

在tableView中,使用

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

继承NSCell:

@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;
}

然后重写方法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;
}

最后,在tableView中使用子类化的cell。

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;
	}
}
另外,设置选中状态字体颜色等可以放在以下方法中执行。

- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(int)rowIndex



                     


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值