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

标签 swift uicollectionview uicollectionviewcell tvos uicollectionviewflowlayout

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

例如: 如果细胞数是4

enter image description here

如果细胞数是5

enter image description here

如果细胞数是9

enter image description here

如果细胞数是11

enter image description here

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

我的代码是:

//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)
}

实际上,如果单元格数量更多,我需要 N x (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)
        }
    }
}

像这样从 Storyboard 集部分插入。

enter image description here

输出

enter image description here

关于swift - 动态修复屏幕中的 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55158966/

相关文章:

swift - 未调用完成处理程序 - Alamofire/stripe

ios - KVO 观察 AVPlayer 导致 iOS 13 中的 App 崩溃

ios - 阴影未显示在 Collection View 中

ios - 调用 didHighlightItemAtIndexPath 而不调用 UICollectionView 的 didSelectItemAtIndexPath

ios - 自定义UICollectionViewCell占用大量内存

ios - UIDocumentPickerViewController 不调用 didPickDocument 方法

ios - Swift - UIToolbar 项目选择状态

ios - 检查 collectionView 中的文本字段是否为空

ios - 使用多个 UICollectionView,出现错误, "... must be initialized with non-nil layout"

ios - UICollectionView 滚动问题