我正在开发一个 iPad 应用程序,它应该显示 4 个彼此相邻的 CollectionView。 Collection View 的高度应为屏幕的 3/4,因此布局将如下所示:
___________________
| top content |
|-------------------|
| CV | CV | CV | CV |
|____|____|____|____|
我试图缩小范围并仅使用其中一个 Collection View 创建一个新项目,但我一直遇到同样的问题:当我点击其中一个单元格时,所有单元格都消失了。重现:
- 使用 Swift 作为语言使用模板“Single View Application”创建一个新项目
- 设置 Storyboard:
- 在 Storyboard中拖动一个新的 Collection View Controller
- 将 Storyboard 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://dl.dropboxusercontent.com/u/607872/CollectionViewBugZip.zip
谁能告诉我我做错了什么?
最佳答案
事实证明错误在于将 Collection View 添加为 subview ,这被认为不是好的做法。显然,当仅添加 subview 时,它与其 View Controller “断开连接”。
这成功了: View Controller .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)
}
关于ios - 自定义 Collection View 单元格在点击时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42137454/