我想要做的是在单击I按钮时在NSTableView中设置所选行的背景颜色.我看到那里的人使用过的其他案件tableView:willDisplayCell:forTableColumn:row:
和setBackgroundColor:
,但我不认为这会在我的情况,我想点击一个按钮,当它发生的工作.
我知道我可以使用NSTableView的selectedRow
方法找到所选行并为单元格设置背景颜色setBackgroundColor:
,但我不知道该怎么办是从NSInteger获取所选行到NSCell来设置背景颜色.
NSTableView
NSCell
每个列只使用一个实例.绘制内容时,将为每一行更新单元格.这就是为什么没有方法来获取指定行的单元格 - 你必须修改单元格tableView:willDisplayCell:forTableColumn:row:
.
您可以告诉表视图使用只更新一行reloadDataForRowIndexes:columnIndexes:
.
将背景颜色设置为NSTableview
Row
- (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]];
}
Run Code Online (Sandbox Code Playgroud)
使用这种方法,我们可以为每一行提供不同的颜色.确保drawsBackground
启用,NSTextFieldCell
否则这将没有任何效果!
如果您只想更改整个选定行的颜色:
NSInteger selectedRow = [self.nsTableView selectedRow];
NSTableRowView* rowView = [self.nsTableView rowViewAtRow:selectedRow makeIfNecessary:NO];
[rowView setBackgroundColor:[NSColor blackColor]];
Run Code Online (Sandbox Code Playgroud)
glhf
归档时间: |
|
查看次数: |
8836 次 |
最近记录: |