集合视图单元格不出现 [英] Collection View Cells not appearing

查看:51
本文介绍了集合视图单元格不出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示与 buttons 一样多的 collectionViewCells 和我数组中的字符串.但是当我启动模拟器时,只有 CollectionView 的背景,但没有显示任何单元格.可能是什么错误?

I want to display as many collectionViewCells with buttons as there are strings in my array. but when I start the simulator there is just the background of the CollectionViewbut no cells shown. What could be the error?

这是我 CollectionViewController 中的代码,我将它附加到main.storyboard中的 CollectionView 上:

Here is the code from my CollectionViewController that I attached to the CollectionView in the main.storyboard:

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
}

}

这些是Collection View Controller的连接:

These are the connections of the Collection View Controller:

情节提要上的View Controller:

The View Controller on the Storyboard:

推荐答案

您是否将 CollectionViewController 设置为情节提要标识检查器?:)

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

在更改 viewDidLoad 方法中的数据后,我将尝试调用 reloadData().

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

希望有帮助

这篇关于集合视图单元格不出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆