Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为NSTableView中的行着色_Objective C_Cocoa - Fatal编程技术网

Objective c 为NSTableView中的行着色

Objective c 为NSTableView中的行着色,objective-c,cocoa,Objective C,Cocoa,我要做的是,当单击I按钮时,在NSTableView中设置所选行的背景色。 我见过其他一些情况,人们使用了tableView:willDisplayCell:forTableColumn:row:和setBackgroundColor:,但我不认为这在我希望在单击按钮时发生的情况下起作用 我知道我可以使用NSTableView的selectedRow方法找到所选行,并使用setBackgroundColor:设置单元格的背景色,但我不知道如何从NSInteger获取所选行的NSCell以设置其背

我要做的是,当单击I按钮时,在NSTableView中设置所选行的背景色。 我见过其他一些情况,人们使用了
tableView:willDisplayCell:forTableColumn:row:
setBackgroundColor:
,但我不认为这在我希望在单击按钮时发生的情况下起作用


我知道我可以使用NSTableView的
selectedRow
方法找到所选行,并使用
setBackgroundColor:
设置单元格的背景色,但我不知道如何从NSInteger获取所选行的NSCell以设置其背景色。

NSTableView
对每列仅使用一个NSCell的实例。绘制内容时,将更新每行的单元格。这就是为什么没有方法获取指定行的单元格的原因。您必须在
tableView:willDisplayCell:forTableColumn:row:
中修改单元格


您可以使用
reloadDataForRowIndexes:columnIndexes:

命令表视图仅更新一行,以将背景色设置为
NSTableview

  - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell1 forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 
{
      if(row==1)
         [cell1 setBackgroundColor:[NSColor redColor]];
      else if(row==2||row==3)
         [cell1 setBackgroundColor:[NSColor greenColor]];
      else
         [cell1 setBackgroundColor:[NSColor clearColor]];
}
使用这种方法,我们可以给每一行赋予不同的颜色。。
确保在
NSTextFieldCell
上启用了
drawsBackground
,否则将无效

如果只想更改整个选定行的颜色:

NSInteger selectedRow = [self.nsTableView selectedRow];
NSTableRowView* rowView = [self.nsTableView rowViewAtRow:selectedRow makeIfNecessary:NO];
[rowView setBackgroundColor:[NSColor blackColor]];

glhf

我刚刚试过,但它将每一行都涂成灰色,而不仅仅是我重新加载的那一行。1。不要使用
int
。使用
NSInteger
。2.试着照尼古拉说的做。(注意它需要雪豹)。使您的
tableView:willDisplayCell:forTableColumn:row:
方法同时检查行是否被选中以及用户是否单击了按钮。1。好啊2 & 3. 我不能完全按照他说的做,因为我有一个NSOutlineView,或者我可以这样做,因为NSOutlineView继承了NSTableView?当然,既然NSOutlineView是一个NSTableView,你可以用它做任何NSTableView可以做的事。(继承==“是一个”)好的,这是我现在得到的,但是我不确定如何判断用户是否单击了按钮。仅当您使用的是基于视图的表时。如果我们讨论的是基于单元格的表,就像我认为的那样,那么rowViewAtRow:。。。方法将始终返回nil。