基于视图的NSTableView选择突出显示 [英] View based NSTableView selection highlighting
问题描述
我正在尝试将iOS应用程序移植到Mac上,在过渡期间遇到了两个问题.其中之一是NSTableView
的自定义. NSCell
,NSTableRowView
和基于自定义NSView
的NSTableview
之间到底有什么区别?我最初从基于NSTableView
的视图开始,但是很快我注意到我必须自己处理选择.我无法做到这一点,所以我继续使用NSTableRowView
,奇怪的是,它没有调用自定义NSTableRowView
的初始化程序.
I am trying to port an iOS application onto the Mac and I came across a couple of issues during the transition. One of them is the customization of NSTableView
. What exactly is the difference between NSCell
, NSTableRowView
and custom NSView
based NSTableview
? I initially started out with a view based NSTableView
, but I soon noticed that I would have to handle the selection myself. I could not pull that off, so I went on to use NSTableRowView
, which, strangely, does not call the initialiser of my custom NSTableRowView
.
我基本上只是想要一个带有自定义内容的自定义表格视图单元格,该单元格是可选的.最好的方法是什么?
I basically just want a custom table view cell with custom contents, which is selectable. What is the best way to do it ?
在iOS上,我只是将UITableViewCell
子类化并设置其selectedView属性.在Mac上,这似乎要复杂得多.
On iOS, I would just subclass UITableViewCell
and set its selectedView property. On Mac this seems to be more complicated than that.
推荐答案
我实际上刚刚发现(在边栏中)这个问题,建议将NSTableRowView
子类化.我之前已经做过,但是没有用.我已经再次尝试过,并且非常令人惊讶的是它现在可以工作了……
I have actually just found (in the sidebar) this question, which advises to subclass NSTableRowView
. I had already done that before, but it did not work. I have tried it again and quite surprisingly it works now...
但是,此答案不是很有帮助,因此我将尝试涵盖为完成这项工作而所做的工作.
However, this answer is not very informative, so I will try to cover what I have done in order to make this work.
首先,我实现了以下NSTableView委托方法,并返回nil!:
First of all, I implemented the following NSTableView delegate method and return nil!:
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{
return nil;
}
为了使用基于视图的表(我想NSTableViewRow也被认为是基于视图的表...),您必须实现此方法.我不太确定自己做错了什么,但是如果没有这种方法,我的单元格将不会显示!
In order to use a view based (I guess NSTableViewRow is regarded a view based table as well...) table you HAVE TO implement this method. I am not quite sure what I might have done wrong, but without this method, my cells are not displayed!
通过设置以下属性,确保不让NSTableView处理任何选择:
Make sure to not let the NSTableView handle any selection by setting this property:
yourNSTableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
好的,现在我们要使用以下委托方法设置单元:
Okay, so now we want to set up our cells with the following delegate method:
-(NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row{
static NSString *cellID = @"cell_identifier";
//use this if you want to reuse the cells
CustomTableRowView *result = [tableView makeViewWithIdentifier:cellID owner:self];
if (result == nil) {
result = [[CustomTableRowView alloc] initWithFrame:NSMakeRect(0, 0, self.frame.size.width, 80)];
result.identifier = cellID;
}
result.data = [tableData objectAtIndex:row];
// Return the result
return result;
}
好的,现在子类NSTableRowView
并实现/重写以下两个方法:
Okay so now subclass NSTableRowView
and implement/override the following two methods:
首先,我们必须重写setSelected:
,以使单元格在被选择时重绘其背景.所以这里是:
First we have to override setSelected:
in order to make the cell redraw its background when it is selected. So here it is:
-(void)setSelected:(BOOL)selected{
[super setSelected:selected];
[self setNeedsDisplay:YES];
}
如前所述,我们调用setNeedsDisplay:
以便单元格重绘其背景.
As mentioned earlier, we call setNeedsDisplay:
in order for the cell to redraw its background.
最后,绘制代码.像这样覆盖方法drawBackgroundInRect:
:
Finally, the drawing code. Override the method drawBackgroundInRect:
like this:
-(void)drawBackgroundInRect:(NSRect)dirtyRect{
if (!self.selected) {
[[NSColor clearColor] set];
} else {
[someColor set];
}
NSRectFill(dirtyRect);
}
这篇关于基于视图的NSTableView选择突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!