NSTableView - 初始选择灰色直到点击(聚焦) [英] NSTableView - Initial Selection Grey until Clicked (Focussed)
问题描述
我在这里有一个简单的应用示例,我把它拼凑在一起,我得到的几乎就是我想要的.
I've got a simple example of an app here which I slapped together, and what I'm getting is pretty much what I'm after.
问题是当视图加载时,在 NSViewController 的 viewDidLoad 中,我将 tableView 的选定索引设置为 0,即第一项(有效).
The issue is that when the view loads up, in the NSViewController's viewDidLoad, I set the tableView's selected index to 0 i.e. the first item (which works).
我注意到的是,当发生这种情况时,所选行的颜色显示为灰色(即好像它不是活动窗口/视图)……当我物理单击时,它似乎只以正常的蓝色突出显示在选定的行上.
What I do notice is that when this happens, the selected row comes up as grey in color (i.e. as if it's not an active window/view)… It only seems to high light in the normal blue color when I physically click on the row that's selected.
我可以确认该行被选中并且一切正常.
I can confirm that the row is selected and everything appears fine.
有什么想法吗?
确认一下,我用来选择行的代码是:
To confirm, the code I use to select the row is:
override func viewDidAppear() {
self.tableView.selectRowIndexes(NSIndexSet(index: 0), byExtendingSelection: false)
}
以下是实际视图本身发生的情况:
Here is what's happening with the actual view itself:
上图:较深的灰线是选择栏".这就是视图激活后立即发生的情况.
ABOVE: The darker grey line is the "selection bar". This is what happens as soon as the view becomes active.
上图:一旦我点击那一行(曾经是深灰色的那一行),我就会得到他想要的高光......即海军蓝.
ABOVE: Once I click on that row (the one which was once dark grey), I get he desired high lighting.. i.e. Navy Blue.
推荐答案
cell 是灰色的原因是因为 table view 没有焦点/不是第一响应者.
The reason why the cell is grey is because the table view doesn't have focus / isn't the first responder.
tableView 单元格选择颜色有 3 种状态
There are 3 states for tableView cell selection color
1) 无选择 = 清除行背景
1) no selection = clear row background
2) 选择和焦点 = 蓝色行背景
2) selection and focus = blue row background
3) 选择且无焦点 = 灰色行背景
3) selection and no focus = grey row background
这可能是因为另一个视图具有焦点.简单地选择一个单元格不会将焦点转移到 tableView.你需要调用 NSWindow.makeFirstResponder() 来改变焦点.
This is probably because another view has focus. Simply selecting a cell doesn't shift focus to a tableView. You need to call NSWindow.makeFirstResponder() to change the focus.
func tableViewSelectionDidChange(notification: NSNotification) {
let tableView = notification.object as! NSTableView
if tableView.selectedRow != -1 {
self.window!.makeFirstResponder(self.tableView)
}
}
这篇关于NSTableView - 初始选择灰色直到点击(聚焦)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!