Den*_*nis 3 ios uicollectionviewcell swift
我正在开发一款iPad应用程序,它应该能够显示4个彼此相邻的CollectionView.集合视图的高度应该是屏幕的3/4,因此布局看起来像这样:
___________________
| top content |
|-------------------|
| CV | CV | CV | CV |
|____|____|____|____|
Run Code Online (Sandbox Code Playgroud)
我试图缩小所有内容并创建一个只有其中一个集合视图的新项目,但我仍然遇到同样的问题:当我点击其中一个单元格时,它们都会消失.重现:
CollectionViewCell.swift :(通过ctrl拖动标签创建出口到源代码)
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var label: UILabel!
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
更改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.
}
}
Run Code Online (Sandbox Code Playgroud)
这是iOS模拟器的捕获:
我希望我没有忘记重现问题.为了以防万一,我将项目上传到我的Dropbox.
https://dl.dropboxusercontent.com/u/607872/CollectionViewBugZip.zip
有人能告诉我我做错了什么吗?
Den*_*nis 12
原来错误在于将集合视图添加为子视图,这被认为是不错的做法.显然,当只添加子视图时,它与视图控制器"断开连接".
这就是诀窍: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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1486 次 |
| 最近记录: |