Search code examples
iosswiftuicollectionviewuicollectionviewcellibdesignable

Strange error in storyboard for custom control


This is the error:

Base.lproj/Main.storyboard: error: IB Designables: Failed to update auto layout status: The agent raised a "NSInternalInconsistencyException" exception: could not dequeue a view of kind: UICollectionElementKindCell with identifier WalletCurrencyCollectionCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

I have wallet control in storyboard enter image description here

Wallet control has 4 cells in UICOllectionView , but it gives this error in storyboard (but when I launch code, everything is OK, it just gives error in storyboard). But if I set number of cells = 0 , it works fine, so the problem is in the collection view cell

Here is the structure of the cell

enter image description here

Here is the structure of wallet control

enter image description here

Here is the code of wallet control:

import UIKit

class WalletCurrencyCollectionCell: UICollectionViewCell {
    @IBOutlet var labelAccount: UILabel!

}

@IBDesignable class WalletControl: UIView, UIPickerViewDelegate, UIPickerViewDataSource, UICollectionViewDelegate,     UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet var labelInYourAccount: UILabel!
@IBOutlet var labelOtherAccounts: UILabel!
@IBOutlet var collectionViewMain: UICollectionView!

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    setup()
}

override init(frame: CGRect) {
    super.init(frame: frame)

    setup()
}

func setup() {
    view = loadViewFromNib()
    view.frame = bounds
    view.autoresizingMask = [.FlexibleHeight ,.FlexibleWidth]
    addSubview(view)

    // localizations
    labelInYourAccount.text = "wallet_label_inYourAccount".localized
}

override func awakeFromNib() {
    // register nib
    let collectionCellNib = UINib(nibName: "WalletCurrencyCollectionCell", bundle: nil)
    collectionViewMain.registerNib(collectionCellNib, forCellWithReuseIdentifier: "WalletCurrencyCollectionCell")

}

func loadViewFromNib() -> UIView {
    let bundle = NSBundle(forClass: self.dynamicType)
    let nib = UINib(nibName: "WalletControl", bundle: bundle)
    view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
    return view
}

override func intrinsicContentSize() -> CGSize {
//        return CGSizeMake(200, labelSuper.maxY + 10)
    let sizeOfView = viewContainerToMesherSize.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
    return sizeOfView
}

// MARK: - Collection view

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // test
    return 4
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("WalletCurrencyCollectionCell", forIndexPath: indexPath) as! WalletCurrencyCollectionCell

    // fill with information

    return cell
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    collectionView.deselectItemAtIndexPath(indexPath, animated: true)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    let width = collectionView.widthMy
    return CGSizeMake(width, 100)
}
}

So I don't understand why I have this error in storyboard about nib, because I register nib. And after app launch everything is ok. What can I do with this storyboard error (the problem is in UICollectionView, because if I set number of cell = 0 everything is OK)?


Solution

  • I solved that by calculating count of cells using server responce (I return 0 if there were no server responce about wallet accounts), so in this case storyboard always thinks that I have 0 cell and I don't have any warnings, and when I launch application everything is still OK, because I just properly display cells in UICollectionView

    func help_getArrayOfUserCurrencyAccounts() -> [SCurrencyAccount] {
            if let currentUser = ApiManager.sharedInstance.userService_currentUser {
                return currentUser.arrayCurrencyAccounts
            }
            else {
                return [SCurrencyAccount]()
            }
        }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
            let realAccountsCount = help_getArrayOfUserCurrencyAccounts().count
            if realAccountsCount > 0 {
                if realAccountsCount == 1 {
                    return realAccountsCount
                }
                else {
                    return realAccountsCount + 1 // so we can scroll through accounts
                }
            }
            else {
                return 0
            }
        }