1

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: cell.hidden = YES

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

JozackOverFlow
  • 267
  • 4
  • 18
  • Try this : http://stackoverflow.com/questions/13360975/uicollectionviews-cell-disappearing – Santosh Jun 22 '16 at 18:37
  • @Santosh thanks for your reply. I tried the answers in your link but still got the same result. The cells are now displayed for a little longer but still keep disappearing. Here's the video: https://youtu.be/8H4KqY6JJNs – JozackOverFlow Jun 22 '16 at 19:04
  • Did you use `shouldInvalidateLayoutForBoundsChange` ? – Santosh Jun 22 '16 at 19:11
  • Not sure, if this helps you - http://stackoverflow.com/questions/12927027/uicollectionview-flowlayout-not-wrapping-cells-correctly-ios/13389461#13389461 – Santosh Jun 22 '16 at 19:13
  • @Santosh I tried all the answers in both link you gave but still the same problem for me :( – JozackOverFlow Jun 22 '16 at 19:25

1 Answers1

1

Silly me, I forgot to addChildViewController and didMoveToParentViewController methods. Add those methods after you add subview and everything will be fine.

JozackOverFlow
  • 267
  • 4
  • 18