Skip to content

Commit b9b6912

Browse files
committed
Focus NSTextField works
1 parent d507645 commit b9b6912

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

TableViewDemo/ViewController.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelega
5858
}
5959

6060
func addNewDream() {
61-
dreams.append("Double Click or Press Enter to Add Item")
61+
dreams.append("")
6262
table.beginUpdates()
6363
let last = dreams.count - 1
6464
table.insertRows(at: IndexSet(integer: last), withAnimation: .effectFade)
6565
table.scrollRowToVisible(last)
6666
table.selectRowIndexes([last], byExtendingSelection: false)
6767
table.endUpdates()
68+
69+
// focus on the last item, add cursor & start editing
70+
let keyView = table.view(atColumn: 0, row: last, makeIfNecessary: false) as! NSTableCellView
71+
self.view.window!.makeFirstResponder(keyView.textField)
72+
6873
saveDreams()
6974
}
7075

0 commit comments

Comments
 (0)