自定义集合视图单元格在点击时消失

Custom collection view cell disappears on tap

我正在开发一个 iPad 应用程序,它应该显示 4 个彼此相邻的 CollectionView。集合视图的高度应为屏幕的 3/4,因此布局将如下所示:

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

我试图缩小所有内容的范围并仅使用其中一个集合视图创建了一个新项目,但我一直 运行 遇到同样的问题:当我点击其中一个单元格时,所有单元格都消失了.重现:

CollectionViewCell.swift:(通过按住 ctrl 键拖动标签到源代码来创建出口)

import UIKit

class CollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var label: UILabel!

}

CollectionViewController.swift:(注意viewDidLoad实现中的注释)

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
    }

}

更改 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.
    }


}

这是 iOS 模拟器的截图:

https://youtu.be/VVBsTnYLGM4

希望我没有忘记重现问题的任何内容。为了以防万一,我将项目上传到我的保管箱。

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

谁能告诉我我做错了什么?

原来错误是将集合视图添加为子视图,这不是好的做法。显然,当仅添加子视图时,它是来自其视图控制器的 "disconnected"。

这成功了: 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)
}