在NSTableCellView中使NSTextField firstResponder() [英] make NSTextField in NSTableCellView firstResponder()
问题描述
供那些不喜欢阅读20条评论的人寻找答案.这是对我有用的东西:
- tableView是基于视图的,而不是基于单元格的(属性检查器)
- tableView.reloadData()不够快.使用insertRow(at [0]:withAnimation:.leftSlide)可以正常工作
- 确保NSTextField和NSTextFieldCell是可编辑的(属性检查器)
最终代码:
tableViewAlarms.insertRows(at: [0], withAnimation: .effectFade)
let keyView = tableViewAlarms.view(atColumn: 0, row: 0, makeIfNecessary: false) as! NSTableCellView
self.view.window!.makeFirstResponder(keyView.textField)
问题:
有一个问题.我被困了一段时间使我的NSTableView以我想要的方式工作.我是一个初学者,使用类,继承和视图会给我带来很多麻烦.
got one question. I have been stuck for some time making my NSTableView work the way I want it to. I'm a beginner and working with classes, inheritance, and views is giving me a lot of trouble.
您可以通过按"+"按钮向NSTableView添加一行.
you add a row to the NSTableView by pushing the '+' button.
第一个tableColumn中的NSTextField是可编辑的,并在双击时开始编辑
the NSTextField in the first tableColumn is editable and begins editing when double-clicked
现在,我希望添加行时自动激活第一列中的NSTextField(显示光标,响应通过键盘输入的文本).
now I want the NSTextField in the first column to be activated automatically (show cursor, responding to text input via keyboard) when a row is added.
到目前为止,我已经尝试过:
so far i have tried:
class NSTextFieldSubClass: NSTextField {
override func becomeFirstResponder() -> Bool {
return true
}
}
我还尝试处理单元的绑定(或者精确地说是NSTextField本身),但是由于我真的不知道如何解决这些问题,所以无济于事
I also played around with the bindings of the cell (or the NSTextField itself to be precise), but since I don't really know my way around those it went nowhere
我认为要走的路是
在创建单元格时使NSTextField成为firstResponder,但我不知道如何.任何帮助都将不胜感激!
make the NSTextField the firstResponder when the cell is created, but I don't know how. Any help is greatly appreciated!!
还:当光标闪烁时,如何精确地调用NSTextField的状态?
also: how exactly is the state of an NSTextField called when the cursor is blinking?
对于子类,这是我尝试过的所有内容:
as for the subclass, this is all I tried:
class NSTextFieldSubClass: NSTextField {
override func becomeFirstResponder() -> Bool {
return true
}
}
编辑屏幕截图:
edit2:
edit3:
edit4:
推荐答案
不要子类
Don't subclass NSTextField
. You can make the text field in a view based table view first responder right out of the box.
这很容易.
假设您知道row
和column
(在您的示例中,第0行和第0列)使用view(atColumn: 0, row
获取表格单元格视图,并将文本字段设为第一响应者.
Assuming you know row
and column
(in your example row 0 and column 0) get the table cell view with view(atColumn: 0, row
and make the text field first responder.
let keyView = tableView.view(atColumn: 0, row: 0, makeIfNecessary: false) as! NSTableCellView
view.window!.makeFirstResponder(keyView.textField)
第二个出口是做什么的?只需使用标准的默认textField
.
And what is the second outlet for? Just use the standard default textField
.
这篇关于在NSTableCellView中使NSTextField firstResponder()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!