-1

我正在快速编写一个应用程序,当我在 iPhone 模拟器上运行测试应用程序时,一切正常,但随后我尝试另一个选项卡,它崩溃并在控制台日志中显示此错误报告。

斯威夫特 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)

用户集合视图控制器

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
    }
}
4

1 回答 1

1

你得到一个例外,因为你的UICollectionView

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

您正在尝试使用标识符使单元格出列CollectionViewCell,但是没有使用该标识符注册的类/单元格。

您的选择是

  1. 在代码/情节提要中创建具有该标识符的单元格。
  2. 更改您的标识符collectionView(_:cellForItemAt:)以匹配确实存在的标识符。

我的猜测是您在某处输入了错误的标识符,请确保它是正确的。

于 2016-11-28T23:31:20.740 回答