首页 > 解决方案 > 错误:superview:设置 tableView.delegate 和 tableView.datasource 等于 self 时发送到实例的无法识别的选择器

问题描述

错误:-[Handsome_Fuel.AddVehicle superview]: unrecognized selector sent to instance 0x7fcb55c1dc30

这是完整的错误消息:

2019-10-29 15:46:55.442464-0400 Handsome Fuel[8417:104074] -[Handsome_Fuel.AddVehicle superview]: unrecognized selector sent to instance 0x7f9f5bd33440
2019-10-29 15:46:55.449755-0400 Handsome Fuel[8417:104074] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Handsome_Fuel.AddVehicle superview]: unrecognized selector sent to instance 0x7f9f5bd33440'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23baa1ee __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff50864b20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23bcb154 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKitCore                           0x00007fff475a6e79 -[UIResponder doesNotRecognizeSelector:] + 302
    4   CoreFoundation                      0x00007fff23baef6c ___forwarding___ + 1436
    5   CoreFoundation                      0x00007fff23bb10f8 _CF_forwarding_prep_0 + 120
    6   UIKitCore                           0x00007fff47a4b796 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 142
    7   UIKitCore                           0x00007fff477038d7 -[UITableViewCell _addSubview:positioned:relativeTo:] + 402
    8   UIKitCore                           0x00007fff47a1f625 -[UIView initWithCoder:] + 1131
    9   UIKitCore                           0x00007fff477045f3 -[UITableViewCell initWithCoder:] + 85
    10  Handsome Fuel                       0x00000001089ee885 $s13Handsome_Fuel11VehicleCellC5coderACSgSo7NSCoderC_tcfc + 341
    11  Handsome Fuel                       0x00000001089ee963 $s13Handsome_Fuel11VehicleCellC5coderACSgSo7NSCoderC_tcfcTo + 51
    12  UIKitCore                           0x00007fff471e7350 -[UIClassSwapper initWithCoder:] + 2427
    13  UIFoundation                        0x00007fff46a37cd6 UINibDecoderDecodeObjectForValue + 744
    14  UIFoundation                        0x00007fff46a379e1 -[UINibDecoder decodeObjectForKey:] + 251
    15  UIKitCore                           0x00007fff471eb657 -[UIRuntimeConnection initWithCoder:] + 125
    16  UIFoundation                        0x00007fff46a37cd6 UINibDecoderDecodeObjectForValue + 744
    17  UIFoundation                        0x00007fff46a37f7d UINibDecoderDecodeObjectForValue + 1423
    18  UIFoundation                        0x00007fff46a379e1 -[UINibDecoder decodeObjectForKey:] + 251
    19  UIKitCore                           0x00007fff471e641e -[NSCoder(UIIBDependencyInjectionInternal) _decodeObjectsWithSourceSegueTemplate:creator:sender:forKey:] + 450
    20  UIKitCore                           0x00007fff471e918d -[UINib instantiateWithOwner:options:] + 1145
    21  UIKitCore                           0x00007fff4775ec7d -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 610
    22  Handsome Fuel                       0x00000001089e62d5 $s13Handsome_Fuel10AddVehicleC9tableView_12cellForRowAtSo07UITableF4CellCSo0kF0C_10Foundation9IndexPathVtF + 709
    23  Handsome Fuel                       0x00000001089e65d5 $s13Handsome_Fuel10AddVehicleC9tableView_12cellForRowAtSo07UITableF4CellCSo0kF0C_10Foundation9IndexPathVtFTo + 165
    24  UIKitCore                           0x00007fff47778d93 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 781
    25  UIKitCore                           0x00007fff4774210c -[UITableView _updateVisibleCellsNow:] + 3081
    26  UIKitCore                           0x00007fff477621df -[UITableView layoutSubviews] + 194
    27  UIKitCore                           0x00007fff47a52ad5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
    28  QuartzCore                          0x00007fff2b06e91d -[CALayer layoutSublayers] + 255
    29  QuartzCore                          0x00007fff2b073323 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
    30  QuartzCore                          0x00007fff2b07fa7c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
    31  QuartzCore                          0x00007fff2afc6e54 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
    32  QuartzCore                          0x00007fff2affc32f _ZN2CA11Transaction6commitEv + 643
    33  UIKitCore                           0x00007fff475723b4 _UIApplicationFlushRunLoopCATransactionIfTooLate + 104
    34  UIKitCore                           0x00007fff47614cba __handleEventQueueInternal + 6902
    35  CoreFoundation                      0x00007fff23b0d271 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    36  CoreFoundation                      0x00007fff23b0d19c __CFRunLoopDoSource0 + 76
    37  CoreFoundation                      0x00007fff23b0c974 __CFRunLoopDoSources0 + 180
    38  CoreFoundation                      0x00007fff23b0767f __CFRunLoopRun + 1263
    39  CoreFoundation                      0x00007fff23b06e66 CFRunLoopRunSpecific + 438
    40  GraphicsServices                    0x00007fff38346bb0 GSEventRunModal + 65
    41  UIKitCore                           0x00007fff47578dd0 UIApplicationMain + 1621
    42  Handsome Fuel                       0x00000001089ef27b main + 75
    43  libdyld.dylib                       0x00007fff516ecd29 start + 1
    44  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)

我只在将 tableView 委托和数据源设置为 self 并在 ViewDidLoad() 中运行此函数时出现此错误,如下所示:

func SetTableViewDelegates() {
    tableView.delegate = self
    tableView.datasource = self
}

当我删除此功能并通过故事板将委托和数据源链接到主 ViewController 来执行相同操作时,也会发生这种情况。

我已经确定这是造成它的原因。当它被删除时,我可以像平常一样打开该选项卡而不会崩溃。我觉得这可能与主 ViewController 和 tableView 之间有一个 View 有关,但我不确定。

我有一个 ViewController 类的扩展,它合并了 DataSource 和 TableViewDelegate 协议,如下所示:

extension AddVehicle: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return vehicles.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let vehicle = vehicles[indexPath.row]
        print(vehicles[indexPath.row])
        let cell = tableView.dequeueReusableCell(withIdentifier: "vehicleCell") as! VehicleCell

        cell.SetVehicle(vehicle: vehicle)
        return cell
    }

}

我一直在密切关注教程,只是更改了初始数据。一切都很好,直到分配了数据源和委托的部分,我不知道该去哪里找。

标签: swifttableview

解决方案


推荐阅读