【问题标题】:UICollectionView only shows the last cellUICollectionView 只显示最后一个单元格
【发布时间】:2014-11-20 21:24:47
【问题描述】:

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

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
}

【问题讨论】:

  • 为什么要设置单元格的框架?
  • 另外,请发布您的其他集合视图委托和数据源方法。我还注意到您发布的代码中没有使用array
  • 我将单元格的框架设置为更大。是的,我知道这只是一个示例数组,我将对其进行更改
  • 不要设置单元格的框架。使用您的集合视图布局对象使其更大。
  • 感谢它正在工作

标签: swift uicollectionview


【解决方案1】:

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

【讨论】: