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:
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?
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With