I'm facing this weird problem with UICollectionView. I created a custom view controller which contain a UICollectionView by xib file. Then I add the custom view controller to the main view and fill data for the collection view after added. But every time the view is shown, the collection view flashes and then it show blank, no item?! It's really really weird, I've never seen it before?! You can see it in the video below: https://youtu.be/Pwn8cufpQ1Y
Here's how I add the custom view:
let list = HorizontalListViewController(nibName: "HorizontalListViewController", bundle: nil)
list.view.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(list.view)
let constraintTop = NSLayoutConstraint(item: list.view, attribute: .Top, relatedBy: .Equal, toItem: self.contentView, attribute: .Top, multiplier: 1, constant: self.contentViewHeight.constant)
let constraintLead = NSLayoutConstraint(item: list.view, attribute: .Leading, relatedBy: .Equal, toItem: self.contentView, attribute: .Leading, multiplier: 1, constant: 0)
let constraintTrail = NSLayoutConstraint(item: list.view, attribute: .Trailing, relatedBy: .Equal, toItem: self.contentView, attribute: .Trailing, multiplier: 1, constant: 0)
let constraintHeight = NSLayoutConstraint(item: list.view, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: CGFloat(LIST_VIEW_HEIGHT))
list.view.addConstraint(constraintHeight)
self.contentView.addConstraints([constraintTop, constraintLead, constraintTrail])
list.setList(datas, title: title)
Register the cell in viewDidLoad:
// Do any additional setup after loading the view.
self.collectionView.dataSource = self
self.collectionView.delegate = self
self.collectionView.registerNib(UINib(nibName:"HorizontalListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: CELL_IDENTIFIER)
Did you see this problem before? How can I solve it?
EDIT 1:
I tried the solution which given by Santosh but the result is still the same, but now the cell show up a little bit longer before disappearing. Here's the video: https://youtu.be/8H4KqY6JJNs
EDIT 2:
I tried to create the custom view controller by using storyboard, but cells still keep disappearing
EDIT 3:
I install FLEX and use it to debug the UI and here's what I got:
As you can see, the hidden property is set to YES. I double check every line of code and I'm sure that I didn't set the hidden property of the cell. So I think that at some point, some where, the collection view auto set its cell to be hidden