如何指定集合视图中的列数?

时间:2017-08-13 03:01:54

标签: ios swift collectionview

这也可能是每行的单元格数量,这更容易。

我的集合视图代码是:

let reuseIdentifier = "cell"

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell

    cell.myLabel.text = String(self.items[indexPath.item])

    return cell
}

在MyCollectionViewCell中使用它:

class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
}

我希望每行(或列)的单元格数量为items.count的三分之一,并且有3行。我尝试过播放每个部分的部分和项目数量,以及单元格宽度。如果单元格宽度可以最好,那么它适用于所有设备。

提前致谢!

编辑:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSize(width: collectionView.bounds.size.width/score1.count)
}

0 个答案:

没有答案
相关问题