当我运行应用程序时,CollectionView不会显示

时间:2018-01-03 10:28:49

标签: ios swift uicollectionview swift4

我创建了这个collectionView enter image description here

我的故事板中已经填充了不同的元素,这是viewController,其中collectionView是

 import UIKit

    class CollectionClass: UIViewController {

        @IBOutlet weak var CollectionView: UICollectionView!

        override func viewDidLoad() {
            super.viewDidLoad()

            // Do any additional setup after loading the view.
        }

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

    }

现在我试图运行应用程序但是为什么不显示collectionView?

1 个答案:

答案 0 :(得分:0)

试试这个 如果这个代码只有三个单元格,那么

import UIKit

class CollectionClass: UIViewController,UICollectionViewDataSource {

    @IBOutlet weak var CollectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        CollectionView.dataSource = self

        // Do any additional setup after loading the view.
    }

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

    func numberOfSections(in collectionView: UICollectionView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1 // number of section

    }


    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of items
    return 5 //number of items 

    }

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

    let cellId = String()
    switch(indexPath.row) {
         case 0: cellId = "cellidOne"
         case 1: cellId = "cellidTwo"
         case 2: cellID = "cellidThree" 
    }
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)

    // Configure the cell

    return cell
}

}