gpt4 book ai didi

ios - cellForItemAtIndexPath 从未调用过

转载 作者:行者123 更新时间:2023-11-28 08:54:42 26 4
gpt4 key购买 nike

我有一个 UICollectionView,我将它添加到 Storyboard中的 UIViewController 中。

它包含 BookCell 类的 UICollectionViewCell 和可重用标识符 BookCell

bookArray 从之前的 ViewController 中传入 prepareForSegue 方法

cellForItemAtIndexPath 永远不会被调用。我试图在 Storyboard 和 viewDidLoad 中将 BookCollectionVC 声明为委托(delegate)和数据源,如您所见已被注释掉,但这并没有改变任何东西。

我已经阅读了多个与单元格大小、本节中的项目数、声明 delegatedataSource 的多种方式有关的 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)?

关于ios - cellForItemAtIndexPath 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33403147/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com