2016-11-28 51 views
-1

我在swift中編寫應用程序,當我在iPhone模擬器上運行測試應用程序時,一切正常,但後來我嘗試到另一個選項卡,崩潰並在控制檯日誌中顯示此錯誤報告。libC++ abi.dylib:終止於類型爲NSException的未捕獲異常錯誤

斯威夫特3/Xcode的8.1 ​​/ iOS的10.1

2016-11-28 22:36:23.440 BiOda[70323:2714339] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.5.2/UICollectionView.m:4922 
2016-11-28 22:36:23.445 BiOda[70323:2714339] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 
*** First throw call stack: 
(
0 CoreFoundation 0x000000010a98a34b __exceptionPreprocess + 171 
1 libobjc.A.dylib 0x0000000109fce21e objc_exception_throw + 48 
2 CoreFoundation 0x000000010a98e442 +[NSException raise:format:arguments:] + 98 
3 Foundation 0x0000000109b64e4d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 
4 UIKit 0x000000010be1538b -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 1960 
5 UIKit 0x000000010be15834 -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:] + 169 
6 BiOda 0x000000010853016c _TFC5BiOda29UsersCollectionViewController14collectionViewfTCSo16UICollectionView13cellForItemAtV10Foundation9IndexPath_CSo20UICollectionViewCell + 188 
7 BiOda 0x0000000108530897 _TToFC5BiOda29UsersCollectionViewController14collectionViewfTCSo16UICollectionView13cellForItemAtV10Foundation9IndexPath_CSo20UICollectionViewCell + 87 
8 UIKit 0x000000010be00980 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 467 
9 UIKit 0x000000010be007a7 -[UICollectionView _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 35 
10 UIKit 0x000000010be05c7f -[UICollectionView _updateVisibleCellsNow:] + 4803 
11 UIKit 0x000000010be0b913 -[UICollectionView layoutSubviews] + 313 
12 UIKit 0x000000010b582f50 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237 
13 QuartzCore 0x00000001102fccc4 -[CALayer layoutSublayers] + 146 
14 QuartzCore 0x00000001102f0788 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
15 QuartzCore 0x00000001102f0606 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
16 QuartzCore 0x000000011027e680 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 
17 QuartzCore 0x00000001102ab767 _ZN2CA11Transaction6commitEv + 475 
18 QuartzCore 0x00000001102ac0d7 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 113 
19 CoreFoundation 0x000000010a92ee17 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23 
20 CoreFoundation 0x000000010a92ed87 __CFRunLoopDoObservers + 391 
21 CoreFoundation 0x000000010a913b9e __CFRunLoopRun + 1198 
22 CoreFoundation 0x000000010a913494 CFRunLoopRunSpecific + 420 
23 GraphicsServices 0x000000010e485a6f GSEventRunModal + 161 
24 UIKit 0x000000010b4be964 UIApplicationMain + 159 
25 BiOda 0x000000010852990f main + 111 
26 libdyld.dylib 0x000000010d74768d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

UsersCollectionViewController

import UIKit 
import FirebaseDatabase 
private let reuseIdentifier = "CollectionViewCell" 
class UsersCollectionViewController: UICollectionViewController { 
    @IBOutlet weak var aivLoading: UIActivityIndicatorView! 

    var databaseRef = FIRDatabase.database().reference() 
    var usersDict = NSDictionary() 

    var userNamesArray = [String]() 
    var userImagesArray = [String]() 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.aivLoading.startAnimating() 
     self.databaseRef.child("user_profile").observe(.value, with: { 
      (snapshot) in 

      self.usersDict = snapshot.value as! NSDictionary 

      for(userId, details) in self.usersDict{ 


       let img = (details as AnyObject).object(forKey: "profile_pic_small") as! String 
       let name = (details as AnyObject).object(forKey: "name") as! String 
       let firstName = name.components(separatedBy: " ")[0] 

       self.userImagesArray.append(img) 
       self.userNamesArray.append(firstName) 
       self.collectionView?.reloadData() 

       self.aivLoading.stopAnimating() 


      } 


     }) 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    override func numberOfSections(in collectionView: UICollectionView) -> Int { 
     // #warning Incomplete implementation, return the number of sections 
     return 1 
    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return self.userImagesArray.count 
    } 

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

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell 


     let imageUrl = NSURL(string:userImagesArray[indexPath.row]) 

     let imageData = NSData(contentsOf: imageUrl! as URL) 

     cell.userImage.image = UIImage(data:imageData! as Data) 
     cell.userName.text = userNamesArray[indexPath.row] 

     return cell 
    } 
} 

回答

0

你得到一個例外,因爲你的UICollectionView

could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewCell 

您正試圖將標識符爲CollectionViewCell的單元出列,但沒有使用該標識符註冊的類/單元。

您的選項是要麼

  1. 創建代碼/故事板一個具有該標識符細胞。
  2. 更改您的collectionView(_:cellForItemAt:)中的標識符以與存在的標識符相匹配。

我的猜測是你錯誤地輸入了標識符,請確認它是正確的。

+0

謝謝。對不起我遲到了.. –

相關問題