我正在尝试以编程方式创建UICollection视图,实际上我已经使用storyboard成功地创建了2个UICollection但是第三个我想以编程方式创建它并且看起来效果不好,我已经使用了UICollectioanViewCell
名称为TesCollectionViewCell。 cellForItemAtIndexPath
不起作用,但奇怪的是numberOfItemsInSection
效果更好
这是我的代码:
class TesCollectionView: UIControl, UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
var uiCollectionView : UICollectionView
var collHuh = ["Happy", "Feet", "Cool"]
override init(frame: CGRect)
{
println("heyyyyyyyyyy")
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .Horizontal
layout.itemSize = CGSize(width: 150, height: 150)
uiCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
uiCollectionView.registerClass(TesCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
super.init(frame: frame)
uiCollectionView.dataSource = self
uiCollectionView.delegate = self
addSubview(uiCollectionView)
layer.borderWidth = 2
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{println("Does works better")
return 10
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{ println("it works now")
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! TesCollectionViewCell
cell.imageView.image = UIImage(named: "image")
cell.backgroundColor = UIColor.blueColor()
return cell
}
required init(coder aDecoder: NSCoder)
{
uiCollectionView = UICollectionView(frame: CGRectZero)
super.init(coder: aDecoder)
}
}
我的代码出了什么问题?
答案 0 :(得分:1)
确保您的手机框架尺寸不是0,0!
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
答案 1 :(得分:-2)
设置部分中的项目数。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
?