Why won't my Collection View Cells display in

2019-03-05 23:46发布

I am trying to programmatically create a Collection View with six cells. The iPhone Simulator in Xcode 8 told me that my build was successful. There is still an error though.

Any help spotting the bug or bugs that are causing my cells not to display in the Simulator is greatly appreciated. (I put the Collection View directly into the IB from the Object Library. Also, I want each cell to occupy the entire size of the Collection View, hence displayedCellDimensions, because the user will segue between cells when a forward or backward button is tapped.) Thank you!

import UIKit

class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {



//MARK: - Collection View Properties
@IBOutlet weak var ibCollectionView: UICollectionView!

var cellArray:[customCollectionViewCell] = Array(repeating: customCollectionViewCell(), count: 6)

let displayedCellDimensions = CGSize(width: 343, height: 248)



//MARK: - Xcode-generated Methods
override func viewDidLoad() {
    super.viewDidLoad()


    // Do any additional setup after loading the view.
    ibCollectionView.dataSource = self
    ibCollectionView.delegate = self


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//MARK: - Collection View Methods
//Place cells in collection view and change the cell size
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    if collectionView.visibleCells.isEmpty {
        for cell in 0...cellArray.count {
            collectionView.addSubview(cellArray[cell])
            return CGSize(width: 343, height: 248)
        }
    }
    return CGSize(width: 343, height: 248)
}

//Register a new cell
func registerCell(_ collectionView: UICollectionView) {
    for _ in 0...cellArray.count {
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
    }
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
    return cell
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return cellArray.count


}
...}

1条回答
祖国的老花朵
2楼-- · 2019-03-06 00:17

In your viewDidLoad() Try:

viewDidLoad() {
ibCollectionView.dataSource = self
ibCollectionView.delegate = self
}

Add this to the top of your class:

class FluidIntakeMainMenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

You must use these functions in your ViewController class:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {}
查看更多
登录 后发表回答