【问题标题】:Divide the number of cell between two prototypes在两个原型之间划分单元格的数量
【发布时间】:2019-05-22 20:57:06
【问题描述】:

因为我是初学者,所以我遇到的问题可能不是那么专业。 我的应用中有两个原型集合视图单元格,我们称它们为 Cell1Cell2,它们都在同一个部分中。

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as? customCell {
        return cell
    } else if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as? customCell {
        return cell
    }
    return UICollectionViewCell()
}

我的问题从这里开始: 在numberOfItemsInSection 中,我想说例如从原型Cell1 绘制3 个单元格,从Cell2 绘制5 个单元格。 我不知道如何在两个原型之间划分单元数,有人可以帮我吗?

【问题讨论】:

    标签: swift uitableview uicollectionview


    【解决方案1】:

    你可以试试

    override func collectionView(_ collectionView: UICollectionView, 
           numberOfItemsInSection section: Int) -> Int {
         return 8
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        if (0...2).contains(indexPath.item) { 
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell1", for: indexPath) as! customCell {
            return cell 
        } else { 
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell2", for: indexPath) as? customCell {
            return cell
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您有多种选择。如果你知道你将拥有多少个细胞,假设 8 个,你可以做一个简单的切换。

      switch indexPath.item {
          case 0,1,2,3:
               let cell = .....
               return cell
           case 4,5,6,7:
               let cell2 = ....
                return cell
           default:
                let cell = ...
                return cell
      }
      

      您还可以使用模数方法,这将为您提供偶数项目编号的 cell1 和不均匀的 cell2。 在不直接了解你的意图的情况下,我只能给你一个笼统的答案。

      【讨论】:

      • 非常感谢您的帮助
      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 2015-06-05
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多