当我构建我的项目时,屏幕在模拟器中显示为白色( 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/