Reputation: 241
I have created a custom UICollectionViewCell in Interface Builder, When I run the project, the UICollectionViewCell's subViews are nil, subviews doesn't show.
I checked on the Internet, someBody say " **I am calling
self.collectionView.registerClass(YNAskQuestionTextCollectionViewCell.self, forCellWithReuseIdentifier: "Cell_Ask_Qustion_text").
If you are using a storyboard you don't want to call this. It will overwrite what you have in your storyboard.**"
Then I remove registerClass
. But I get error as below
init?(coder aDecoder: NSCoder)
of custom UICollectionViewCell , the error isfatal error: init(coder:) has not been implemented: file /Users/nongmeng/Desktop/nongjitong/nongjitong/class/home/conreoller/ask question/YNAskQuestionTextCollectionViewCell.swift, line 48
.
If I use code to add subViews, they show,Everything works fine. I wnat know Why.Why are subViews Of UICollectionViewCell in Interface Builder nil. Anyone can help me? Here is the example project. https://github.com/chengyanan/UICollectionView
This is viewController
//MARK: life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.registerClass(YNAskQuestionTextCollectionViewCell.self, forCellWithReuseIdentifier: "Cell_Ask_Qustion_text")
self.collectionView.registerClass(YNAskQuestionImageCollectionViewCell.self , forCellWithReuseIdentifier: "Cell_Ask_Qustion_Image")
self.collectionView.registerClass(YNAskQuestionLocationCollectionViewCell.self , forCellWithReuseIdentifier: "Cell_Ask_Qustion_Location")
let flow = UICollectionViewFlowLayout()
flow.minimumInteritemSpacing = 6
flow.minimumLineSpacing = 16
let size = CGSizeMake(self.view.frame.size.width, 100)
print(size)
flow.itemSize = size
flow.sectionInset = UIEdgeInsetsMake(0, 0, 30, 0)
flow.scrollDirection = UICollectionViewScrollDirection.Vertical
self.collectionView.collectionViewLayout = flow
self.collectionView.backgroundColor = kRGBA(234, g: 234, b: 234, a: 1)
self.collectionView.bounces = true
}
//MARK:UICollectionViewDataSource
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let identify = "Cell_Ask_Qustion_text"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identify, forIndexPath: indexPath) as! YNAskQuestionTextCollectionViewCell
return cell
} else if indexPath.section == 2 {
let identify = "Cell_Ask_Qustion_Location"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identify, forIndexPath: indexPath) as! YNAskQuestionLocationCollectionViewCell
return cell
}
let identify = "Cell_Ask_Qustion_Image"
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(identify, forIndexPath: indexPath) as! YNAskQuestionImageCollectionViewCell
return cell
}
this is custom UICollectionViewCell
import UIKit
class YNAskQuestionTextCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var textView: UITextView!
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.greenColor()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
Upvotes: 2
Views: 1578
Reputation: 13020
If you are using a nib file then you need to register nib file. check the below code
Change your code for registerClass
to registerNib
self.collectionView?.registerNib(UINib(nibName: "yourcellclass", bundle: nil), forCellWithReuseIdentifier: "identifier")
Upvotes: 4