UICollectionViewCell 没有出现
UICollectionViewCell not appearing
我正在以编程方式创建 UICollectionView
。
作为免责声明,我遵循了本教程:http://randexdev.com/2014/07/uicollectionview/
所以我的设置如下:
我有一个带有 UIScrollView
的故事板,它由 ViewControllerA
控制。
在一个单独的 class CustomCollectionViewController
中,我有以下代码:
import Foundation
import UIKit
class CustomCollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
super.init(collectionViewLayout: layout)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
//collectionView = UICollectionView(frame: CGRectMake(0, 0, 500, 500), collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.backgroundColor = UIColor.redColor()
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
self.view.addSubview(collectionView!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.greenColor()
return cell
}
}
在我的 ViewControllerA
我有这个代码
let collectionViewController: CustomCollectionViewController = CustomCollectionViewController(nibName: nil, bundle: nil)
collectionViewController.view.frame.origin = CGPointMake(scrollViewWidth*3, 0)
scrollView.addSubview(collectionViewController.view)
如果在模拟器中 运行,我可以看到 UICollectionView
的红色背景,但没有任何单元格。
任何提示我做错了什么?
可能是-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
不见了!
问题是我没有在 viewControllerA
中将 ViewController
设置为 childViewController
self.addChildViewController(collectionViewController)
collectionViewController.didMoveToParentViewController(self)
我正在以编程方式创建 UICollectionView
。
作为免责声明,我遵循了本教程:http://randexdev.com/2014/07/uicollectionview/
所以我的设置如下:
我有一个带有 UIScrollView
的故事板,它由 ViewControllerA
控制。
在一个单独的 class CustomCollectionViewController
中,我有以下代码:
import Foundation
import UIKit
class CustomCollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
layout.itemSize = CGSize(width: 90, height: 120)
super.init(collectionViewLayout: layout)
collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
//collectionView = UICollectionView(frame: CGRectMake(0, 0, 500, 500), collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.backgroundColor = UIColor.redColor()
collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
self.view.addSubview(collectionView!)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 14
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath)
cell.backgroundColor = UIColor.greenColor()
return cell
}
}
在我的 ViewControllerA
我有这个代码
let collectionViewController: CustomCollectionViewController = CustomCollectionViewController(nibName: nil, bundle: nil)
collectionViewController.view.frame.origin = CGPointMake(scrollViewWidth*3, 0)
scrollView.addSubview(collectionViewController.view)
如果在模拟器中 运行,我可以看到 UICollectionView
的红色背景,但没有任何单元格。
任何提示我做错了什么?
可能是-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
不见了!
问题是我没有在 viewControllerA
ViewController
设置为 childViewController
self.addChildViewController(collectionViewController)
collectionViewController.didMoveToParentViewController(self)