swift - UICollectionView 只显示最后一个单元格

标签 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
}

最佳答案

不要设置单元格的框架。使用您的 Collection View 布局对象使其变大。

关于swift - UICollectionView 只显示最后一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27049835/

相关文章:

ios - 多次打开sqlite数据库还是单次打开sqlite数据库?

swift 4 - HMCharacteristicTypeSerialNumber 弃用

ios - 在非滚动 UICollectionView 中动态调整 UICollectionViewCell 的大小

ios - 通过 UICollectionView 单元格快速枚举 - Swift

uitableview - UITableView中水平UICollectionView的 TableView 单元格大小

ios - 如何使用点击手势从 View 与 Controller 对话

ios - UICollectionViewCell 在滚动时可见

ios - 为什么我无法从类的实例中获取属性?

ios - PickerView selectedValue anyObject 返回 nil

ios - 带文件上传的摘要式身份验证