自定义控件的故事板中的奇怪错误
Strange error in storyboard for custom control
这是错误:
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
我在故事板中有钱包控制
钱包控件在 UICOllectionView 中有 4 个单元格,但它在故事板中给出了这个错误(但是当我启动代码时,一切正常,它只是在故事板中给出了错误)。但是如果我设置 number of cells = 0 ,它工作正常,所以问题出在集合视图 cell
这是单元格的结构
这里是钱包控件的结构
这里是钱包控制的代码:
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)
}
}
所以我不明白为什么我在故事板中出现关于 nib 的错误,因为我注册了 nib。应用启动后一切正常。我该如何处理这个故事板错误(问题出在 UICollectionView 中,因为如果我设置单元格数 = 0 一切正常)?
我通过使用服务器响应计算单元格数解决了这个问题(如果没有关于钱包帐户的服务器响应,我 return 0),所以在这种情况下,故事板总是认为我有 0 个单元格,但我没有'有任何警告,当我启动应用程序时,一切仍然正常,因为我只是在 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
}
}
这是错误:
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
我在故事板中有钱包控制
钱包控件在 UICOllectionView 中有 4 个单元格,但它在故事板中给出了这个错误(但是当我启动代码时,一切正常,它只是在故事板中给出了错误)。但是如果我设置 number of cells = 0 ,它工作正常,所以问题出在集合视图 cell
这是单元格的结构
这里是钱包控件的结构
这里是钱包控制的代码:
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)
}
}
所以我不明白为什么我在故事板中出现关于 nib 的错误,因为我注册了 nib。应用启动后一切正常。我该如何处理这个故事板错误(问题出在 UICollectionView 中,因为如果我设置单元格数 = 0 一切正常)?
我通过使用服务器响应计算单元格数解决了这个问题(如果没有关于钱包帐户的服务器响应,我 return 0),所以在这种情况下,故事板总是认为我有 0 个单元格,但我没有'有任何警告,当我启动应用程序时,一切仍然正常,因为我只是在 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
}
}