1

I have a UICollectionView that I added to my UIViewController in my Storyboard.

It contains a UICollectionViewCell of class BookCell and reusable identifier BookCell.

bookArray gets passed in the prepareForSegue method from the previous ViewController

cellForItemAtIndexPath never gets called. I have attempted to declare BookCollectionVC as delegate and dataSource both in Storyboard and in viewDidLoad, as you can see commented out, but that does not change anything.

I have read multiple SO answers pertaining to cell size, to the number of items in the section, to the multiple ways of declaring delegate and dataSource and have tried/double checked all of them.

Any ideas?

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)
    }
7
  • can you please check the items into bookArray array ?
    – deoKasuhal
    Oct 28, 2015 at 23:11
  • What do you mean check the items into ? @Dev
    – ray john
    Oct 28, 2015 at 23:14
  • If there is no items into bookArray then cellForItemAtIndexPath is never is going to call. In the sample code you assigned bookArray as empty array. You need to make sure that bookArray has some object in order to cellForItemAtIndexPath method is being executed.
    – deoKasuhal
    Oct 28, 2015 at 23:18
  • Does the line return UICollectionViewCell() ever get called? Also have you registered the cell called BookCell?
    – totiDev
    Oct 28, 2015 at 23:19
  • He doesnt have to register it if it exists in the storyboard inside the collection view. As others asks, are u sure u have any items in ur array?
    – hasan
    Oct 29, 2015 at 0:04

1 Answer 1

0

As a test, don't use an if-let. Just get the cell value and print it. What is its type? Have you properly assigned the cells to be the custom class (BookCell) in your StoryBoard?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.