自定义集合视图单元格随时消失

时间:2017-02-09 13:01:05

标签: ios swift uicollectionviewcell

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

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

我试图缩小所有内容,只使用其中一个集合视图创建了一个新项目,但我仍然遇到同样的问题:当我点击其中一个单元格时,所有这些单元格都会消失。重现:

  • 使用模板"单一视图应用程序"创建一个新项目。使用Swift作为语言
  • 设置故事板:
    • 在故事板中拖动新的集合视图控制器
    • 将故事板ID设置为" CollectionViewController"
    • 表示单元格:将标识符设置为MyCollectionViewCell,在单元格中拖动标签,设置约束
  • 使用以下源代码创建文件:

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

我希望我没有忘记重现问题。为了以防万一,我将项目上传到我的Dropbox。

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

有人能告诉我我做错了吗?

1 个答案:

答案 0 :(得分:11)

原来错误在于将集合视图添加为子视图,这被认为是不错的做法。显然,当只添加子视图时,它与视图控制器“断开连接”。

这就是诀窍: 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)
}