2017-07-26 80 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

相关问题