繁体   English   中英

无法在捆绑包中加载 NIB:NSBundle 在 iOS Swift 中?

[英]Could not load NIB in bundle: NSBundle in iOS Swift?

我正在研究框架。 在框架内部,我在 Xib 中创建了UIViewController -> UICollectionView 为 UICollectionView 单元创建了分离的 Xib。 并将单元格注册为“TaskCollectionViewCell”

我尝试为collectionView注册笔尖它崩溃并说Could not load NIB in bundle: NSBundle

这是我的 UIViewController -> UICollectionView 的代码:

  override public func viewDidLoad() {
      super.viewDidLoad()

     self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(UINib(nibName: "TaskCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "TaskCollectionViewCell")

     }

     public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 10
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TaskCollectionViewCell", for: indexPath) as! TaskCollectionViewCell

    return cell

}

控制台错误:

   2019-11-12 12:08:00.487285+0530 A8FlowSampleApp[35271:1648394] *** 
 Terminating app due to uncaught exception 
  'NSInternalInconsistencyException', reason: 'Could not load NIB in 
   bundle: 'NSBundle 
   </Users/A8/Library/Developer/CoreSimulator/Devices/135B1A20-4329-44AA-B2C4-F0426F6BD131/data/Containers/Bundle/Application/79CA4693-3DCF-44A2-A441-36CCBB822894/A8FlowSampleApp.app> (loaded)' with name 'TaskCollectionViewCell''
  *** First throw call stack:
 (
0   CoreFoundation                      0x00007fff23c4f02e __exceptionPreprocess + 350
1   libobjc.A.dylib                     0x00007fff50b97b20 objc_exception_throw + 48
2   CoreFoundation                      0x00007fff23c4ee6c +[NSException raise:format:] + 188
3   UIKitCore                           0x00007fff474b6553 -[UINib instantiateWithOwner:options:] + 487
4   UIKitCore                           0x00007fff4703ec9e -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 874
5   UIKitCore                           0x00007fff4703f48d -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 88
6   A8Flow                              0x00000001063b42f5 $s6A8Flow6MyTaskC14collectionView_13cellForItemAtSo012UICollectionF4CellCSo0kF0C_10Foundation9IndexPathVtF + 277
7   A8Flow                              0x00000001063b4415 $s6A8Flow6MyTaskC14collectionView_13cellForItemAtSo012UICollectionF4CellCSo0kF0C_10Foundation9IndexPathVtFTo + 165
8   UIKitCore                           0x00007fff4702980a -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 416
9   UIKitCore                           0x00007fff47029664 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31
10  UIKitCore                           0x00007fff4702eefc -[UICollectionView _updateVisibleCellsNow:] + 6332
11  UIKitCore                           0x00007fff47033f7c -[UICollectionView layoutSubviews] + 351
12  UIKitCore                           0x00007fff47d34cfd -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
13  QuartzCore                          0x00007fff2b138d41 -[CALayer layoutSublayers] + 255
14  QuartzCore                          0x00007fff2b13ef33 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
15  UIKitCore                           0x00007fff47d1fe75 -[UIView(Hierarchy) layoutBelowIfNeeded] + 980
16  UIKitCore                           0x00007fff471369e1 -[UINavigationController _layoutViewController:] + 1465
17  UIKitCore                           0x00007fff4712fa92 -[UINavigationController _layoutTopViewControllerLookForNested:] + 589
18  UIKitCore                           0x00007fff4712dc32 -[UINavigationController navigationTransitionView:didEndTransition:fromView:toView:] + 750
19  UIKitCore                           0x00007fff47ce95d4 -[UINavigationTransitionView _notifyDelegateTransitionDidStopWithContext:] + 410
20  UIKitCore                           0x00007fff47ce98a6 -[UINavigationTransitionView _cleanupTransition] + 623
21  UIKitCore                           0x00007fff47d2917f +[UIView(UIViewAnimationWithBlocks) conditionallyAnimate:withAnimation:layout:completion:] + 107
22  UIKitCore                           0x00007fff47ce9189 -[UINavigationTransitionView transition:fromView:toView:] + 2414
23  UIKitCore                           0x00007fff471378c6 -[UINavigationController _startTransition:fromViewController:toViewController:] + 2758
24  UIKitCore                           0x00007fff47137d56 -[UINavigationController _startDeferredTransitionIfNeeded:] + 868
25  UIKitCore                           0x00007fff471390c1 -[UINavigationController __viewWillLayoutSubviews] + 150
26  UIKitCore                           0x00007fff47119ef7 -[UILayoutContainerView layoutSubviews] + 217
27  UIKitCore                           0x00007fff47d34cfd -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2478
28  QuartzCore                          0x00007fff2b138d41 -[CALayer layoutSublayers] + 255
29  QuartzCore                          0x00007fff2b13ef33 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 517
30  QuartzCore                          0x00007fff2b14a86a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 80
31  QuartzCore                          0x00007fff2b0917c8 _ZN2CA7Context18commit_transactionEPNS_11TransactionEd + 324
32  QuartzCore                          0x00007fff2b0c6ad1 _ZN2CA11Transaction6commitEv + 643
33  UIKitCore                           0x00007fff47867481 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 81
34  CoreFoundation                      0x00007fff23bb204c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
35  CoreFoundation                      0x00007fff23bb17b8 __CFRunLoopDoBlocks + 312
36  CoreFoundation                      0x00007fff23bac644 __CFRunLoopRun + 1284
37  CoreFoundation                      0x00007fff23babe16 
CFRunLoopRunSpecific + 438
38  GraphicsServices                    0x00007fff38438bb0 
GSEventRunModal + 65
39  UIKitCore                           0x00007fff4784fb68 
 UIApplicationMain + 1621
40  A8FlowSampleApp                     0x0000000105cba6bb main + 75
41  libdyld.dylib                       0x00007fff51a1dc25 start + 1
42  ???                                 0x0000000000000001 0x0 + 1
)
  libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

修复问题,添加捆绑包后,崩溃停止..

 self.collectionView.register(UINib(nibName: "TaskCollectionViewCell", bundle: Bundle(for: TaskCollectionViewCell.self)), forCellWithReuseIdentifier: "TaskCollectionViewCell")

尽量不要使用NIB。 例如,如果您想将 RXSwift 与 tableview 一起使用,请执行以下操作:

stationsTableView.register(FavoriteTableViewCell.self, forCellReuseIdentifier: "FavoriteCellId")

首先是我们的 Cell 作为 class 和写在标识符中的第二个名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM