ios - Collection View 单元格未出现

标签 ios xcode swift uicollectionviewcell collectionview

我想显示与 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
}

这些是 Collection View Controller 的连接:

connections

Storyboard上的 View Controller :

viewcontroller

最佳答案

您是否将 CollectionViewController 设置为 Storyboard身份检查器? :)

viewDidLoad 方法中更改数据后,我会尝试调用 reloadData()

希望对你有帮助

关于ios - Collection View 单元格未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33881683/

相关文章:

ios - 没有找到从 Main.storyboard 引用的 Storyboard

ios - 试图在释放时加载 View Controller 的 View ... UIAlertController

与未知开发者在 Hackhackons 中进行 iOS 开发?如何将 Xcode 和调试工具分享给没有 OSX 设备的人?

ios - 使 iOS 应用符合 IPV6 标准

ios - 如何在swift中更新 anchor 约束

ios - 我可以删除 Xcode.app/Contents/Developer/下的平台吗?

swift - xcode 导航栏按钮状态

objective-c - 如何在其上绘制全屏叠加层和文本+文本字段?

IOS : Button click not getting registered, xcode 未并行显示代码和 Storyboard

ios - Swift - 只能打印对象的内存地址