2017-07-26 86 views
0

我想讓一個UICollectionViewController顯示一個單元格的網格(我將在稍後添加文本,如果可能的話可能爲textViews)。我有:爲什麼我的UICollectionViewController不顯示任何內容?

class GridViewController : UICollectionViewController{ 

    //(NOW GONE) @IBOutlet weak var gridViewTable = UICollectionViewController() 

    let arr1 : [String] = [] 
    let arr2 : [String] = [] 


    override func viewDidLoad(){ 
     super.viewDidLoad() 
     self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "gridCell") 


    } 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
     return 4 
    } 


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




    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath) 
     cell.backgroundColor = UIColor.red 
     return cell 
    } 
} 

我用界面生成器來連接白色的單元格將轉到gridViewTable。根本沒有出現,即使它運行並編譯。我怎樣才能讓細胞顯示出來?

回答

1

請使用這些代碼:

override func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 4 
} 

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 10 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath as IndexPath) 
    cell.backgroundColor = UIColor.red 
    return cell 
} 
+0

謝謝!只是好奇;爲什麼我的工作沒有? – Derry

+0

@Derry你不是覆蓋已經包含在'UICollectionViewController'中的函數,你自己重新定義它們。當我第一次回答你的問題時,我錯過了。 – Paolo

+0

也在我的故事板上,我將TextView拖放到原型單元格中,因爲我希望單元格填充從2D數組中拉出的文本。我怎樣才能得到這些文字視圖? – Derry

0

你不需要一個UICollectionViewController的插座,這個類本身需要照顧。

另外,請確保Identity Inspector中的Interface Builder中的視圖控制器的類設置爲GridViewController(最右側窗格中左側的第三個選項卡)。

+0

我擺脫了出口的,現在我的程序崩潰,當我嘗試去GridViewController – Derry

+0

務必從Interface Builder中刪除出口過於 – Paolo

+0

感謝,它沒有更長時間的崩潰!但是仍然沒有顯示出來的細胞 – Derry

相關問題