Swift 动态修复屏幕中的UICollectionView单元格

Swift 动态修复屏幕中的UICollectionView单元格,swift,uicollectionview,uicollectionviewcell,tvos,uicollectionviewflowlayout,Swift,Uicollectionview,Uicollectionviewcell,Tvos,Uicollectionviewflowlayout,我正在开发一个tvOS应用程序,我想修复电视屏幕上的所有单元格,而不需要垂直或水平滚动。这里的细胞计数是动态的,这是主要问题。我想这样设置所有单元格 class CardListViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ // UICollectionViewDelegateFlowLayout Delegate method

我正在开发一个tvOS应用程序,我想修复电视屏幕上的所有单元格,而不需要垂直或水平滚动。这里的细胞计数是动态的,这是主要问题。我想这样设置所有单元格

class CardListViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    // UICollectionViewDelegateFlowLayout Delegate method

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let screenWidth = collectionView.bounds.width
        let screenHeight = collectionView.bounds.height

        if 0 ... 4 ~= collectionViewArray.count{
           return CGSize(width: (screenWidth - 30) / 2, height: (screenHeight - 30) / 2)
        }else if 5 ... 9 ~= collectionViewArray.count{
            return CGSize(width: (screenWidth - 40) / 3, height: (screenHeight - 40) / 2)
        }else{
            return CGSize(width: (screenWidth - 50) / 4, height: (screenHeight - 50) / 2)
        }
    }
}
例如: 如果单元格计数为4

如果单元格计数为5

如果单元格计数为9

如果单元格计数为11

此处需要动态设置列和行以及单元格的高度和宽度。(不允许滚动)

我的代码是:

//Example array
var collectionViewArray:[UIColor] = [.brown, .red, .purple, .lightGray, .yellow, .black, .cyan, .magenta, .orange, .purple, .lightGray]

override func viewDidAppear(_ animated: Bool) {
    myCollectionView.delegate = self
    myCollectionView.dataSource = self

}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TVMenuCollectionViewCell
    cell.cellView.backgroundColor = collectionViewArray[indexPath.row]
 return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

    let screenWidth = collectionView.bounds.width
    let screenHeight = collectionView.bounds.height
 return CGSize(width: (screenWidth-50)/4, height: (screenHeight-40)/3)
}
实际上,如果单元格的数量更多,我需要nx(N-1)列和行。

工作代码Swift 4

您需要像这样使用UICollectionViewDelegateFlowLayout方法

class CardListViewController:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{

    // UICollectionViewDelegateFlowLayout Delegate method

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        let screenWidth = collectionView.bounds.width
        let screenHeight = collectionView.bounds.height

        if 0 ... 4 ~= collectionViewArray.count{
           return CGSize(width: (screenWidth - 30) / 2, height: (screenHeight - 30) / 2)
        }else if 5 ... 9 ~= collectionViewArray.count{
            return CGSize(width: (screenWidth - 40) / 3, height: (screenHeight - 40) / 2)
        }else{
            return CGSize(width: (screenWidth - 50) / 4, height: (screenHeight - 50) / 2)
        }
    }
}
从故事板集部分插入如下内容

输出


如果计数是15,你会怎么想?@Nikunj Kumbhani,实际上我只举了一个例子。我想这样,因为计数实际上是“n”很好,但对于15或更多的数字,你想要5 X 3或3 X 5?@Nikunj Kumbhani,列数的第一优先级是高…我认为4x4更好,这意味着超过11,将是4 X n,对吗?