我有一个包含必须位于单元格标签中的字符串的数组:
var sounds = ["a", "b", "c", "d"]
这里是 cellForItemAtIndexPath 的代码:
func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
var cell: MyCollectionViewCell = collectionView?.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as MyCollectionViewCell
cell.frame = CGRectMake(0, 0, 100, 100)
cell.label.text = sounds[indexPath.row]
cell.label.textColor = UIColor.blackColor()
cell.label.font = UIFont(name: "Arial", size: 12)
cell.label.textAlignment = NSTextAlignment.Center
return cell
}
问题是,当我运行应用程序时,只有一个单元格,它显示数组中最后一项的文本。
我认为当我单击 CollectionView 时属性检查器中它说项目是一个 1
有没有办法让项目数量成为 array.count ?
这里是我设置项目数量的地方:
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return sounds.count
}
最佳答案
不要设置单元格的框架。使用您的 Collection View 布局对象使其变大。
关于swift - UICollectionView 只显示最后一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27049835/