UICollectionView仅显示最后一个单元格

时间:2014-11-20 21:24:47

标签: swift uicollectionview

我有一个包含必须在单元格标签中的字符串的数组:

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
}

1 个答案:

答案 0 :(得分:2)

不要设置单元格的框架。使用集合视图布局对象使其更大。