I have a view-based NSOutlineView
configured with a dataSource and delegate. It consists of two columns a custom NSTableCellView
class.
Entries in the outline view are editable by selecting a row and pressing the Return
key. Everything is set up according the docs and works fine most of the time.
However, sometimes when I press Return
while a row is selected, the cell's text field does not enter into edit mode. It just beeps.
I can still enter edit mode by "long clicking" on the text field. For some reason, the Return
key event is sometimes handled differently by the outline view.
I captured two stacktraces:
#1 Working as expected:
You can see that from keyDown()
the event eventually makes the cell's text field the first responder.
#2 Beeping
I put a breakpoint at NSBeep
and you can see that the key even takes a different path this time.
I suspect that maybe the internal state of the outline view or underlying NSTableView
is mixed up...
How can I troubleshoot this from here? Which steps does the outline view take to determine if a row can be edited after the Return key was pressed?
Update
I did some more debugging (thanks to @Willeke for the tip about the breakpoint). I managed to tack it down to this:
- Return key is pressed
NSOutlineView.keydown
- ...
NSOutlineView.selectedRow
- outline view checks which row is selected
NSOutlineView.rowView(atRow row: Int, makeIfNecessary: Bool)
- outline view retrieves the selected row view
NSTableRowView.nextValidKeyView
- current row view is asked for the next valid key view
NSTableRowView.nextKeyView
- Now this returns sometimes
nil
and the app just beeps. Most of the time it returns theNSTableCellView
for that row containing theNSTextField
that is then made first responder and hence editable.
- Now this returns sometimes
Now the question is why is the next key view nil
sometimes? I click on the outline view's row so it does have the focus/is selected...