ios - UICollectionView 没有出现在模拟器中

标签 ios swift uicollectionview uicollectionviewcell xcode6-beta6

当我构建我的项目时,屏幕在模拟器中显示为白色( Collection View 为黑色),当然也没有我想要显示的单元格。

我正在使用 xcode6-beta6,这是用于 ios (ipad) 的。

所以我试图用标签填充 Collection View 。在我的 Storyboard中,我有一个 View Controller ,其中只有一个 Collection View 和一个单元格,我在其中放置了一个标签。我知道我在 Storyboard和我的 View Controller 代码中正确设置了标识符和标签。我知道我也正确设置了我的数据源和委托(delegate)网点。我一直在关注本教程 https://www.youtube.com/watch?v=jiiN9oFH3vE但是它在 xcode 5 和 objective-c 中,所以我不知道在 swift/xcode6 中是否有我没有做的完全不同的事情。

这是我的代码

import UIKit

class FirstViewController: UIViewController,UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var testArray:[String]!

override func viewDidLoad() {
    super.viewDidLoad()
    testArray = ["hello","world","hola","mmg","me","la"]
    // Do any additional setup after loading the view, typically from a nib.
}

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

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


func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int{
        return testArray.count
}
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!{
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as FirstCollectionViewCell

    var label = cell.viewWithTag(1) as UILabel
    label.text = testArray[indexPath.row]
    return cell
}



}

最佳答案

您是否在任何地方将 UICollectionView 委托(delegate)和数据源设置为 self?我在你的代码中没有看到它,这可能是问题所在。您可能在 viewDidLoad 方法中需要它。

关于ios - UICollectionView 没有出现在模拟器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25573594/

相关文章:

ios - steroids.js 如何在默认情况下和加载页面上禁用导航栏

xcode - 使用 Swift Xcode 在 Mac 应用程序中运行终端命令

ios - 类布局

ios - 如何在 Swift 中更新 Parse 用户信息?

ios - 无法加载 bundle : NSBundle in iOS Swift? 中的 NIB

objective-c - UICollectionView 非常慢的自定义布局

ios - 在旋转时调整 UIViewController 内的全屏 View 大小?

objective-c - 替换 nsmutablearray 中的对象

iphone - 当值为空和某个数字时如何在标签中显示字符串值

ios - 使用 Alamofire responseImage IOS swift 从 URLRequest 加载图像