在tableview中插入行并设置textfield first responder

时间:2017-06-14 07:20:51

标签: swift macos cocoa nstableview

我有一个nstableview,包含3列和自定义单元格视图。 使用此代码,我在表格中添加了一个新行:

tableView.beginUpdates()
tableView.insertRows(at: IndexSet(integer: tableView.numberOfRows), withAnimation: .effectFade)
tableView.endUpdates()

let view = tableView.view(atColumn: 1, row: tableView.numberOfRows-1, makeIfNecessary: false) as! CustomCell
view.txtName.becomeFirstResponder()

这项工作近乎完美,但问题是:

行将插入,文本字段成为第一个响应者,我可以在其中写入一个值,但如果我改为焦点到该行的另一个文本字段,我的应用程序崩溃:

2017-06-14 09:20:13.282076+0200 Programm[2206:105520] [General] An uncaught exception was raised
2017-06-14 09:20:13.282112+0200 Programm[2206:105520] [General] Row -1 out of row range [0-0] for rowViewAtRow:createIfNeeded:
2017-06-14 09:20:13.289941+0200 Programm[2206:105520] [General] (
    0   CoreFoundation                      0x00007fffb49ac57b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffc9bf01da objc_exception_throw + 48
    2   AppKit                              0x00007fffb25329fb -[NSTableRowData rowViewAtRow:createIfNeeded:] + 690
    3   AppKit                              0x00007fffb25fdb11 -[NSTableView viewAtColumn:row:makeIfNecessary:] + 28
    4   Programm                            0x000000010002019a _TFC8Programm18DokumentController7controlfTCSo9NSControl20textShouldEndEditingCSo6NSText_Sb + 394
    5   Programm                            0x00000001000227cf _TToFC8Programm18DokumentController7controlfTCSo9NSControl20textShouldEndEditingCSo6NSText_Sb + 79
    6   AppKit                              0x00007fffb25e7ba7 -[NSTextField textShouldEndEditing:] + 161
    7   AppKit                              0x00007fffb25d5f1f -[NSTextView(NSSharing) resignFirstResponder] + 483
    8   AppKit                              0x00007fffb24d7923 -[NSWindow _realMakeFirstResponder:] + 235
    9   AppKit                              0x00007fffb24d77e4 -[NSWindow makeFirstResponder:] + 123
    10  AppKit                              0x00007fffb2773fef -[NSTextView(NSPrivate) _giveUpFirstResponder:] + 261
    11  AppKit                              0x00007fffb25d42b7 -[NSTextView doCommandBySelector:] + 192
    12  AppKit                              0x00007fffb25d41d1 -[NSTextInputContext(NSInputContext_WithCompletion) doCommandBySelector:completionHandler:] + 118
    13  AppKit                              0x00007fffb25b1e00 -[NSKeyBindingManager(NSKeyBindingManager_MultiClients) interpretEventAsCommand:forClient:] + 2218
    14  AppKit                              0x00007fffb2df37b1 __84-[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:]_block_invoke.1096 + 355
    15  AppKit                              0x00007fffb2df35e5 __84-[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:]_block_invoke_3 + 80
    16  AppKit                              0x00007fffb25b9f59 -[NSTextInputContext tryHandleEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 93
    17  AppKit                              0x00007fffb2df3560 __84-[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:]_block_invoke.1086 + 262
    18  HIToolbox                           0x00007fffb3eb689f __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_5 + 70
    19  HIToolbox                           0x00007fffb3eb56e2 ___ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec_block_invoke + 108
    20  AppKit                              0x00007fffb2dec4f8 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke.314 + 2874
    21  AppKit                              0x00007fffb25b3835 __55-[NSTextInputContext handleTSMEvent:completionHandler:]_block_invoke_2 + 80
    22  AppKit                              0x00007fffb25b37b4 -[NSTextInputContext tryHandleTSMEvent_HasMarkedText_withDispatchCondition:dispatchWork:continuation:] + 93
    23  AppKit                              0x00007fffb25b319f -[NSTextInputContext handleTSMEvent:completionHandler:] + 2221
    24  AppKit                              0x00007fffb25b2883 _NSTSMEventHandler + 321
    25  HIToolbox                           0x00007fffb3e5bd85 _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1708
    26  HIToolbox                           0x00007fffb3e5aff6 _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 428
    27  HIToolbox                           0x00007fffb3e5ae3f SendEventToEventTargetWithOptions + 43
    28  HIToolbox                           0x00007fffb3eb26d6 SendTSMEvent_WithCompletionHandler + 408
    29  HIToolbox                           0x00007fffb3eb2bb1 __SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler_block_invoke + 400
    30  HIToolbox                           0x00007fffb3eb2a12 __SendFilterTextEvent_WithCompletionHandler_block_invoke + 215
    31  HIToolbox                           0x00007fffb3eb2727 SendTSMEvent_WithCompletionHandler + 489
    32  HIToolbox                           0x00007fffb3eb2511 SendFilterTextEvent_WithCompletionHandler + 236
    33  HIToolbox                           0x00007fffb3eb21d6 SendUnicodeTextAEToUnicodeDoc_WithCompletionHandler + 284
    34  HIToolbox                           0x00007fffb3eb1f8c __utDeliverTSMEvent_WithCompletionHandler_block_invoke_2 + 296
    35  HIToolbox                           0x00007fffb3eb1e32 __utDeliverTSMEvent_WithCompletionHandler_block_invoke + 439
    36  HIToolbox                           0x00007fffb3eb1bf9 TSMKeyEvent_WithCompletionHandler + 632
    37  HIToolbox                           0x00007fffb3eb1948 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_4 + 251
    38  HIToolbox                           0x00007fffb3eb1775 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_3 + 281
    39  HIToolbox                           0x00007fffb3eb148b __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke_2 + 308
    40  HIToolbox                           0x00007fffb3eb11c2 __TSMProcessRawKeyEventWithOptionsAndCompletionHandler_block_invoke + 300
    41  HIToolbox                           0x00007fffb3eb073a TSMProcessRawKeyEventWithOptionsAndCompletionHandler + 3604
    42  AppKit                              0x00007fffb2df33d1 __84-[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:]_block_invoke.1077 + 116
    43  AppKit                              0x00007fffb2df2689 __204-[NSTextInputContext tryTSMProcessRawKeyEvent_orSubstitution:dispatchCondition:setupForDispatch:furtherCondition:doubleSpaceSubstitutionCondition:doubleSpaceSubstitutionWork:dispatchTSMWork:continuation:]_block_invoke.1003 + 121
    44  AppKit                              0x00007fffb2df24f3 -[NSTextInputContext tryTSMProcessRawKeyEvent_orSubstitution:dispatchCondition:setupForDispatch:furtherCondition:doubleSpaceSubstitutionCondition:doubleSpaceSubstitutionWork:dispatchTSMWork:continuation:] + 285
    45  AppKit                              0x00007fffb2df2e1b -[NSTextInputContext _handleEvent:options:allowingSyntheticEvent:completionHandler:] + 1515
    46  AppKit                              0x00007fffb2df2362 -[NSTextInputContext _handleEvent:allowingSyntheticEvent:] + 114
    47  AppKit                              0x00007fffb25b128b -[NSView interpretKeyEvents:] + 232
    48  AppKit                              0x00007fffb25b10a1 -[NSTextView keyDown:] + 704
    49  AppKit                              0x00007fffb2d132cc -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 4086
    50  AppKit                              0x00007fffb2d11f0a -[NSWindow(NSEventRouting) sendEvent:] + 541
    51  AppKit                              0x00007fffb2b974a8 -[NSApplication(NSEvent) sendEvent:] + 4768
    52  AppKit                              0x00007fffb2411427 -[NSApplication run] + 1002
    53  AppKit                              0x00007fffb23dbe0e NSApplicationMain + 1237
    54  Programm                            0x000000010001388d main + 13
    55  libdyld.dylib                       0x00007fffca4d1235 start + 1
    56  ???                                 0x0000000000000003 0x0 + 3
)

1 个答案:

答案 0 :(得分:0)

问题解决了!! 在EndEditing我期望一个选定的行。但如果我添加一行,则不会选中此行。我将以下行添加到我的addRow函数,现在它可以工作:

tableView.selectRowIndexes(IndexSet(integer: tableView.numberOfRows-1), byExtendingSelection: false)