xcode 在 UICollectionView (swift 3) 中注册多个单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43971382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:58:23  来源:igfitidea点击:

Registering multiple cells in UICollectionView (swift 3)

iosswiftxcodeuicollectionview

提问by Stefan

For UICollectionViews, is it possible to have multiple cell types?

对于UICollectionViews,是否可能有多种细胞类型?

For example:

例如:

media_cell

regular_cell

ad_cell

Would you just have to register them or do you have to include an if statement and change the layout according to some variable to achieve this effect.

您是否只需要注册它们,或者您是否必须包含一个 if 语句并根据某些变量更改布局以实现此效果。

For example:

例如:

if cell.ad == true {

}

The reason being, I want a slightly different sized cell for when an image is present. I guess resizing the cell could work but I haven't seen anything on this online.

原因是,当图像存在时,我想要一个大小略有不同的单元格。我想调整单元格的大小可以工作,但我在网上没有看到任何内容。

Any suggestions

有什么建议

回答by Nebojsa Nadj

Try this: 1. Register two(or more) cells. 2.Configure cellforItem for each. 3. Configure sizeForItem for each. First:

试试这个: 1. 注册两个(或更多)单元格。2.为每个配置cellforItem。3. 为每个配置 sizeForItem。第一的:

self.collectionView.register(SmallCell.self, forCellWithReuseIdentifier: "smallCell")
self.collectionView.register(BigCell.self, forCellWithReuseIdentifier: "bigCell")

And then:

进而:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     if dataSource[indexPath.item].hasImage {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “smallCell”, for: indexPath) as! SmallCell
        let model = dataSource[indexPath.item]
        cell.model = model
        return cell
      } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: “bigCell”, for: indexPath) as! BigCell
        let model = dataSource[indexPath.item]
        cell.model = model
        return cell
       }   
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
     if dataSource[indexPath.item].hasImage {
        return CGSize(width: collectionView.frame.width, height: cellHeight+100)
     } else {   
        return CGSize(width: collectionView.frame.width, height: cellHeight)    
     }       
}

回答by Kamil Harasimowicz

Two things and everything should be works:

两件事,一切都应该有效:

  1. You can register any number of cells in one collection view.
  2. Check out self-sizing cells topic and you should not worry about different sizes of cells.
  1. 您可以在一个集合视图中注册任意数量的单元格。
  2. 查看自调整单元格主题,您不必担心不同大小的单元格。

Great tutorial

很棒的教程

More info in this brilliant answer here

此处的精彩答案中的更多信息

Good luck :)

祝你好运 :)