【发布时间】:2016-09-13 03:24:01
【问题描述】:
我以编程方式创建了一个 collectionView(没有情节提要)。我将背景颜色设置为红色,并且显示正确。但是细胞没有显示出来。有谁知道为什么单元格没有显示?
private let cellId = "cellId"
class InfoViewShow: NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
private let cellHeight:CGFloat = 80
var collectionView: UICollectionView?
let layout = UICollectionViewFlowLayout()
override init() {
super.init()
setupCollectionview()
collectionView?.registerClass(InfoCell.self, forCellWithReuseIdentifier: cellId)
}
private func setupCollectionview() {
if let view = UIApplication.sharedApplication().keyWindow {
//let y = (view.frame.width * 10 / 16) + 34 + 26
collectionView = UICollectionView(frame: .zero , collectionViewLayout: layout)
collectionView?.backgroundColor = UIColor.redColor()
collectionView?.delegate = self
collectionView?.dataSource = self
collectionView?.frame = CGRectMake(0, 0, view.frame.width, view.frame.height) // y to y
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath)
cell.backgroundColor = UIColor.blackColor()
return cell
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSizeMake(collectionView.frame.width, cellHeight)
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(5, 5, 5, 5); //top,left,bottom,right
}
}
【问题讨论】:
-
经常出现的问题是数据源和delegate没有设置或者size太小,加断点看看是否调用了cellForItem。您可以尝试将这些方法导出到扩展,有时它可以工作
-
我尝试在两个 cellforitems 和 numberofitems 上放置一些断点,但它们都没有被调用。还有什么建议吗?
-
尝试初始化大小大于零的集合视图
-
我做到了,我得到了相同的结果。细胞仍然没有显示。 collectionView = UICollectionView(frame: CGRectMake(0, 0, view.frame.width, view.frame.height), collectionViewLayout: layout) UICollectionView 显示,红色背景,因为我将颜色更改为红色,但不是单元格.我认为根本没有调用数据源和委托
-
help.apple.com/xcode/mac/8.0/#/devda5478599 在运行时按下“调试视图图”按钮。检查左边的视图。以及右侧面板上的属性。祝你好运
标签: ios swift2 uicollectionview uicollectionviewcell uicollectionviewlayout