Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom collection view cell disappears on tap

I'm working on an iPad app that should display 4 CollectionViews next to each other. The height of the collection views should be 3/4 of the screen, so the layout will look something like this:

 ___________________
|    top content    |
|-------------------|
| CV | CV | CV | CV |
|____|____|____|____|

I tried to narrow everything down and created a new project with just one of these collection views, but I keep running into the same problem: when I tap on one of the cells, all of them disappear. To reproduce:

  • create a new project with template "Single View Application" using Swift as language
  • setup the storyboard:
    • drag a new Collection View Controller in the storyboard
    • set the storyboard ID to "CollectionViewController"
    • for the cell: set identifier to MyCollectionViewCell, drag a label in the cell, set constraints
  • create files with the following source code:

CollectionViewCell.swift: (create the outlet by ctrl-dragging the label to the source code)

import UIKit

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!

}

CollectionViewController.swift: (pay attention to the comment in viewDidLoad implementation)

import UIKit

private let reuseIdentifier = "MyCollectionViewCell"

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        // this has to be removed to work with a custom cell class
        // self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCell
        cell.label.text = "\(indexPath.section)-\(indexPath.row)"
        return cell
    }

}

change the implementation for ViewController.swift:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let frame = view.frame
        let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CollectionViewController")
        vc.view.frame = CGRect(x: 0.0, y: frame.size.height * 0.25, width: frame.size.width, height: frame.size.height * 0.75)
        self.view.addSubview(vc.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Here's a capture of the iOS simulator:

https://youtu.be/VVBsTnYLGM4

I hope I didn't forget anything to reproduce the problem. Just in case, I uploaded the project to my dropbox.

https://dl.dropboxusercontent.com/u/607872/CollectionViewBugZip.zip

Can someone tell me what I'm doing wrong?

like image 543
Dennis Avatar asked Dec 06 '22 15:12

Dennis


1 Answers

Turns out the mistake was in adding the collection view as a subview, which isn't considered good practice. Appearently, when only the subview is added, it's "disconnected" from its view controller.

This did the trick: ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let frame = view.frame
    let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "CollectionViewController")
    self.addChildViewController(vc) // <----------- !!!!
    vc.didMove(toParentViewController: self) // <-- !!!!
    vc.view.frame = CGRect(x: 0.0, y: frame.size.height * 0.25, width: frame.size.width, height: frame.size.height * 0.75)
    self.view.addSubview(vc.view)
}
like image 84
Dennis Avatar answered Dec 25 '22 17:12

Dennis