为什么我的 cellForItemAtIndexPath 不起作用,但 numberOfItemsInSection 在 swift 中效果更好?
Why my cellForItemAtIndexPath doesn't works but numberOfItemsInSection works better in swift?
我正在尝试以编程方式制作 UICollection 视图,实际上我已经使用故事板成功制作了 2 个 UICollection,但我想以编程方式制作第三个 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)
}
}
我的代码有什么问题?
设置部分中的项目数。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
?
确保您的单元格框架大小不是 0,0!
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
我正在尝试以编程方式制作 UICollection 视图,实际上我已经使用故事板成功制作了 2 个 UICollection,但我想以编程方式制作第三个 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)
}
}
我的代码有什么问题?
设置部分中的项目数。
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
?
确保您的单元格框架大小不是 0,0!
func collectionView(collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}