[英]collectionView:cellForItemAtIndexPath: never gets called
[英]cellForItemAtIndexPath never called
我有一個UICollectionView
,它已添加到Storyboard中的UIViewController
中。
它包含了UICollectionViewCell
類BookCell
和可重復使用的標識符BookCell
。
bookArray
從先前的ViewController通過prepareForSegue
方法傳遞
cellForItemAtIndexPath
永遠不會被調用。 我已經嘗試在Storyboard和viewDidLoad
都將BookCollectionVC
聲明為委托和dataSource,就像您看到的已注釋掉一樣,但這並沒有改變。
我已經閱讀了多個有關單元格大小,本節中的項目數,聲明delegate
和dataSource
的多種方式的SO答案,並嘗試/仔細檢查了所有這些答案。
有任何想法嗎?
class BookCollectionVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collection: UICollectionView!
var bookArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// collection.delegate = self
// collection.dataSource = self
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("BookCell", forIndexPath: indexPath) as? BookCell {
// This never gets called
let bookIsbn = bookArray[indexPath.row]
cell.configureCell(bookIsbn)
return cell
} else {
return UICollectionViewCell()
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print(bookArray.count)
return bookArray.count
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let cellWidth = screenWidth / 4
return CGSizeMake(cellWidth,cellWidth)
}
作為測試,請勿使用if-let。 只需獲取單元格值並打印即可。 它是什么類型? 您是否已在StoryBoard中正確地將單元分配為自定義類(BookCell)?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.