我有UITableView
个自定义UITableViewCell
,其中包含UITextField
个。
我想在用户点击单元格时显示键盘。我正在做这样的事情:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "FormCell", for: indexPath) as? FormTableViewCell else {
return
}
cell.textField.becomeFirstResponder()
}
但是当我在iPad上运行该应用程序时,点击该单元格并尝试使用"关闭键"关闭键盘,我收到此错误:
[Assert]没有重复使用表格单元格的索引路径
这是我得到的整个错误:
* thread #1: tid = 0x7f323, 0x00000001021d5cd8 libswiftFoundation.dylib`static Foundation.IndexPath._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSIndexPath>) -> Foundation.IndexPath + 56, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1021d5cd8)
* frame #0: 0x00000001021d5cd8 libswiftFoundation.dylib`static Foundation.IndexPath._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSIndexPath>) -> Foundation.IndexPath + 56
frame #1: 0x00000001010ab9ec PullToDismiss`@objc PullToDismiss.tableView(UITableView, didEndDisplaying : UITableViewCell, forRowAt : IndexPath) -> () + 104 at PullToDismiss+Proxy.swift:0
frame #2: 0x000000018fb29c9c UIKit`-[UITableView _reuseTableViewCell:withIndexPath:didEndDisplaying:] + 304
frame #3: 0x000000018fa13b58 UIKit`-[UITableView _setIsAncestorOfFirstResponder:] + 1028
frame #4: 0x000000018f8bc288 UIKit`+[UIView(Internal) _setIsResponderAncestorOfFirstResponder:startingAtFirstResponder:] + 216
frame #5: 0x000000018f8bc02c UIKit`-[UIWindow _setFirstResponder:] + 112
frame #6: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #7: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #8: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #9: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #10: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #11: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #12: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #13: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #14: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #15: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #16: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #17: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #18: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #19: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #20: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #21: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #22: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #23: 0x000000018fb973bc UIKit`-[UIResponder _setFirstResponder:] + 80
frame #24: 0x000000018fae6ec0 UIKit`-[UIView _setFirstResponder:] + 92
frame #25: 0x000000018f928ecc UIKit`-[UITextField _resignFirstResponder] + 300
frame #26: 0x000000018f8ff374 UIKit`-[UIResponder _finishResignFirstResponder] + 296
frame #27: 0x000000019024d2d0 UIKit`-[UITextField _finishResignFirstResponder] + 52
frame #28: 0x000000018f8a36d0 UIKit`-[UIResponder resignFirstResponder] + 284
frame #29: 0x000000018f928bc8 UIKit`-[UITextField resignFirstResponder] + 136
frame #30: 0x000000018fc25978 UIKit`-[UIKeyboardImpl dismissKeyboard] + 544
frame #31: 0x000000018f9be760 UIKit`-[UIKeyboardLayoutStar continueFromInternationalActionForTouchUp:withActions:timestamp:interval:didLongPress:prevActions:executionContext:] + 2544
frame #32: 0x000000018f9bdb7c UIKit`-[UIKeyboardLayoutStar completeSendStringActionForTouchUp:withActions:timestamp:interval:didLongPress:prevActions:executionContext:] + 784
frame #33: 0x000000018f9b1684 UIKit`-[UIKeyboardLayoutStar completeRetestForTouchUp:timestamp:interval:executionContext:] + 4712
frame #34: 0x000000018fc3211c UIKit`__45-[UIKeyboardLayout touchUpTaskForTouchState:]_block_invoke + 228
frame #35: 0x000000018f821b14 UIKit`-[UIKeyboardTaskQueue continueExecutionOnMainThread] + 384
frame #36: 0x000000018fc31dd0 UIKit`-[UIKeyboardLayout _touchEndedProcessingForTouches:] + 344
frame #37: 0x000000018f82c0c4 UIKit`-[UIWindow _sendTouchesForEvent:] + 2484
frame #38: 0x000000018f827328 UIKit`-[UIWindow sendEvent:] + 2988
frame #39: 0x000000018f7f7da0 UIKit`-[UIApplication sendEvent:] + 340
frame #40: 0x000000018ffe175c UIKit`__dispatchPreprocessedEventFromEventQueue + 2736
frame #41: 0x000000018ffdb130 UIKit`__handleEventQueue + 784
frame #42: 0x00000001898f6b5c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
frame #43: 0x00000001898f64a4 CoreFoundation`__CFRunLoopDoSources0 + 524
frame #44: 0x00000001898f40a4 CoreFoundation`__CFRunLoopRun + 804
frame #45: 0x00000001898222b8 CoreFoundation`CFRunLoopRunSpecific + 444
frame #46: 0x000000018b2d6198 GraphicsServices`GSEventRunModal + 180
frame #47: 0x000000018f8627fc UIKit`-[UIApplication _run] + 684
frame #48: 0x000000018f85d534 UIKit`UIApplicationMain + 208
frame #49: 0x00000001003da970 `main + 140 at AppDelegate.swift:15
frame #50: 0x00000001888055b8 libdyld.dylib`start + 4
该应用程序崩溃了。如果我将UITableView
滚动到顶部,它也会崩溃。我怎样才能防止这种情况发生?
答案 0 :(得分:3)
尝试使用此方法获取单元格,您正在做的是创建新单元格。
if let cell = tableView.cellForRow(at: indexPath) as? FormTableViewCell {
cell.becomeFirstResponder
} else {
..dostuff
}