UICollectionView 只显示最后一个单元格 [英] UICollectionView only shows the last cell

查看:30
本文介绍了UICollectionView 只显示最后一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an array with strings that have to be in a cell label:

var sounds = ["a", "b", "c", "d"]

这里是 cellForItemAtIndexPath 的代码:

here is the code of 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
}

问题是,当我运行应用程序时,只有一个单元格,它显示数组中最后一项的文本.

The problem is that when i run the app there is only one cell and it is displaying the text of the last item in the array.

当我单击 CollectionView 时,我认为它在属性检查器中不起作用,因为它说项目是一个 1

I think it is not working cause in the Attribute inspector when i click the CollectionView it says that items is one 1

有没有办法让物品编号为 array.count ?

Is there a way to make the items number to be array.count ?

这里是我设置项目数量的地方:

Here is where i setup the number of items:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    return sounds.count
}

推荐答案

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

Don't set the cell's frame. Make it larger using your collection view layout object.

这篇关于UICollectionView 只显示最后一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆