Swift 错误:superview:将tableView.delegate和tableView.datasource设置为等于self时,无法识别的选择器发送到实例

Swift 错误:superview:将tableView.delegate和tableView.datasource设置为等于self时,无法识别的选择器发送到实例,swift,tableview,Swift,Tableview,错误:-[Fuel.AddVehicle superview]:无法识别的选择器发送到实例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

错误:
-[Fuel.AddVehicle superview]:无法识别的选择器发送到实例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之间有一个视图有关,但我不确定

我有一个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
    }

}

我一直在密切关注教程,只是更改了初始数据。在指定数据源和委托的部分之前,一切都很顺利,我不知道还有什么地方可以查看。

代码中的哪一行触发了异常?如果您不知道,则需要通过打开断点导航器并单击+按钮并选择添加异常断点来启用异常断点。
AddVehicle
是一个
UIViewController
。但它正在调用
UIView
方法:
superview
。因为它没有,它崩溃了,这就是为什么你有这个问题和那个错误消息。您能显示包括stacktrace在内的完整错误消息吗?您是否创建了UIViewController而不是UItableViewController?你换了吗?您似乎错过了InterfaceBuilder中的一个链接。代码中的哪一行触发了异常?如果您不知道,则需要通过打开断点导航器并单击+按钮并选择添加异常断点来启用异常断点。
AddVehicle
是一个
UIViewController
。但它正在调用
UIView
方法:
superview
。因为它没有,它崩溃了,这就是为什么你有这个问题和那个错误消息。您能显示包括stacktrace在内的完整错误消息吗?您是否创建了UIViewController而不是UItableViewController?你换了吗?您似乎错过了InterfaceBuilder中的一个链接。