我在故事板中添加了一个UICollectionView
它包含类UIViewController和可重用标识符UICollectionViewCell
BookCell在前一个ViewController的BookCell方法中传递
bookArray从不被呼叫。我试图在故事板和prepareForSegue中将cellForItemAtIndexPath声明为委托和数据源,正如您所看到的那样,但这不会改变任何事情。
我读过很多关于单元格大小、节中的项目数、声明BookCollectionVCviewDidLoad的多种方式的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。只需获取单元格值并打印即可。它是什么类型的?您是否已将单元格正确分配为故事板中的自定义类(BookCell)?

10-08 05:59