【发布时间】:2017-11-13 05:57:42
【问题描述】:
我尝试在 UICollectionViewCellCell 内创建 UICollectionView,但未调用 func sizeForItemAtIndexPath 和 cellForItemAtIndexPath,这是我的代码:
import UIKit
class HistoryCollectionViewCell: UICollectionViewCell, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource{
static let cellId = "cell"
override init(frame: CGRect) {
super.init(frame: frame)
setUpCollectionView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let appsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
return collectionView
}()
func setUpCollectionView(){
backgroundColor = UIColor.whiteColor()
addSubview(appsCollectionView)
appsCollectionView.delegate = self
appsCollectionView.dataSource = self
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
print("sizeForItem is called")
return CGSizeMake(100, 100)
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
print("cellForItem is called")
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
return cell
}
}
希望有人帮忙
【问题讨论】: