我想显示与 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 的连接:
Storyboard上的 View Controller :
最佳答案
您是否将 CollectionViewController
设置为 Storyboard身份检查器? :)
在 viewDidLoad
方法中更改数据后,我会尝试调用 reloadData()
。
希望对你有帮助
关于ios - Collection View 单元格未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33881683/