【问题标题】:how to return the number of cells for two different cells in uicollectionview programmatically如何以编程方式返回uicollectionview中两个不同单元格的单元格数
【发布时间】:2018-04-27 13:44:24
【问题描述】:

我目前正在从事一个项目,其中我注册了一个 CollectionViewController 和多个单元格。我需要帮助来返回我希望为每个单元格显示的单元格数。

例如我已经注册:

    collectionView?.register(PhotoCell.self, forCellWithReuseIdentifier: picCell)

    collectionView?.register(ArticleCell.self, forCellWithReuseIdentifier: articleCell)

如您所见,我已经注册了两个单元格。如何以编程方式返回 7 个照片单元格和 4 个文章单元格以显示在我的收藏视图中?

 override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 7
}

截至目前,我只能成功归还 7 个光电池。

【问题讨论】:

    标签: ios swift uicollectionview uicollectionviewcell xcode9


    【解决方案1】:
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }   
    
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PhotoCell //Default will never Execute  
        if data == photo (
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PhotoCell
            return cell )
        else if data == article {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! articleCell
            return cell
        }
        return cell
    
        }
    

    使用这种方法来确定细胞类型。这里你的数据,如果它的音频比那个单元格会被使用,如果它的文章比其他单元格会被使用,并且计数返回将是总数据计数。

    【讨论】:

      【解决方案2】:
       override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
      {
          return 11 
      }
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
      {
          if (indexPath.row < 7)
          {
              if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picCell, for: indexPath) as? PhotoCell
              {
                  //your code here
                  return cell
              }
          }
      
          else if (indexPath.row > 6)
          {
      
              //if indexing is needed create new index starting at 0
              let articeCellindex = indexPath.row - 7
      
              if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: articleCell, for: indexPath) as? ArticleCell
              {
                  return cell
              }
          }
      
          else
          {
              return UICollectionViewCell()
          }
      
      
      
      }
      

      Amey 的回答很好,但他们省略了如何以某种方式构建单元格数据,以便两种单元格类型都包含在一个结构中。另一种方法是明确说明哪些行将显示单元格数据。确保 picCell 和 articleCell 重用标识符都引用同一个单元格。

      【讨论】:

      • 你好,我怎样才能让照片单元格变成一个尺寸,而文章单元格变成另一个尺寸?你会采用同样的结构吗?
      猜你喜欢
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多