集合視圖單元格未出現 (Collection View Cells not appearing)


問題描述

集合視圖單元格未出現 (Collection View Cells not appearing)

我想用 buttons 顯示與我的數組中的字符串一樣多的 collectionViewCells。但是當我啟動模擬器時,只有 CollectionView 的背景,但沒有顯示單元格。可能是什麼錯誤?

這是我的 CollectionViewController 中我附加到 main.storyboard 中 CollectionView 的代碼:

class CollectionViewController: UICollectionViewController {

var Array = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    Array = ["1","2","3","4","5"]
}

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

func collectionView(collectionView: UICollectionView, numberOfItemsSection section: Int) ‑> Int {
    return Array.count
}

override func
    collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ‑> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell

        var button = cell.viewWithTag(1) as! UIButton
        button.titleLabel?.text = Array[indexPath.row]

        return cell
}

}

這些是集合視圖控制器的連接:

connections


參考解法

方法 1:

Did you set the CollectionViewController to the storyboard identity inspector? :)

And I would try to call the reloadData() after you change the data in the viewDidLoad method.

Hope that helps

方法 2:

func layoutCells() {
        let layout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 10, right: 10)
        layout.minimumInteritemSpacing = 5.0
        layout.minimumLineSpacing = 5.0
        layout.itemSize = CGSize(width: (UIScreen.mainScreen().bounds.size.width ‑ 40)/3, height: ((UIScreen.mainScreen().bounds.size.width ‑ 40)/3))
        collectionView!.collectionViewLayout = layout
    }

Try this. Call this function from view did load. I think the problem is that your collection is not laid out correctly.

func viewDidLoad() {
    layoutCells()
}

If this works you can modify the layout options to meet your needs.

方法 3:

I had a similar problem, I found this that was somehow restraining the size of my cell. Hope this helps.

Select the CollectionViewCell

Find this values in size inspector

方法 4:

I removed this code from my CollectionViewController.m file

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

and cell get appeared

方法 5:

Problem is with setting the title to button, use the following code.

override func
    collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ‑> UICollectionViewCell {

        var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! UICollectionViewCell

        var button = cell.viewWithTag(1) as! UIButton
        //button.titleLabel?.text = Array[indexPath.row]
        button.setTitle(Array[indexPath.row], forState: UIControlState.Normal)
        return cell
}

Hope it helps

(by maidiPavel SmejkalSubashGes83Asad MehmoodBhavin Kansagara)

參考文件

  1. Collection View Cells not appearing (CC BY‑SA 2.5/3.0/4.0)

#uicollectionviewcell #iOS #XCode #collectionview #swift






相關問題

Sel UICollectionView tidak terlihat (UICollectionView cells are invisible)

UICollectionViewCell裡面的UIWebView不調用Delegate方法 (UIWebView inside UICollectionViewCell does not call Delegate Method)

Bộ sưu tập xem vấn đề thay đổi kích thước iPhone 4 và iPhone 5 (Collection view resizing issue iPhone 4 vs iPhone 5)

集合視圖單元格未出現 (Collection View Cells not appearing)

UICollectionView 單元格大小 ContentView 問題 (UICollectionView cell size ContentView Issue)

在 Swift 中使用 CollectionView 進行網格佈局 (Grid layout with CollectionView in Swift)

如何為collectionviewcell的刪除自定義動畫? (How to custom animation for collectionviewcell's deletion?)

檢測位於另一個 uicollectionview 內的 uicollectionView 的哪個 UICollectionViewCell 在中心 (Detect which UICollectionViewCell of a uicollectionView that is inside another uicollectionview is in the center)

navigationController?.pushViewController 不工作 (navigationController?.pushViewController is not working)

XCode UICollectionViewCell 在模擬器上看起來比在故事板中小得多 (XCode UICollectionViewCell appearing much smaller on simulator than in storyboard)

CollectionViewCell 未出現在 collectionView 中 (CollectionViewCell not appearing in collectionView)

由 LPLinkView 和 UIImageView 組成的單元格的 CollectionView 速度很慢,並且在滾動時會重新加載數據 (CollectionView with Cells made up of LPLinkView's and UIImageView's is Slow and Reloads Data While Scrolling)







留言討論