0
votes

i have an nstableview with 3 columns and a custom cell view. with this code i add a new row to my table:

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()

this works nearly perfect, but the problem is:

The Row will insert, the textfield become the first Responder, i can write a value in it but if i change to focus to another textfield of this row my app crashs:

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
you try to change datasource and reload tableview.Leang Socheat
sry? i dont understand. should your answer be the problem or a solution?Ghost108
is this happening first time - when there are initially no rows in the table? Looks like your error is that you are trying to access row -1. Should tableView.numberOfRows-1 actually be tableView.numberOfRows ?Russell
the tableView has at the beginning 0 rows. now i add a row with an button. the row will insert (number of rows = 1), now i would like to set the textfield of row 0 (0 = the first row which was inserted) as first responder. this works too. but if select another textfield of the same row, the app crashesGhost108
when insert a new row, need to assign unique tag to the textfield maybe (100 + row number). then let view will get from the tagxmhafiz

1 Answers

0
votes

Problem solved!! in the EndEditing i expected an selected row. but if i add a row, this row isn't selected. i add the following line to my addRow function and now it works:

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