加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:29:21 所属栏目:百科 来源:网络整理
导读:对于UICollectionViews,是否可以有多个单元格类型? 例如: media_cellregular_cellad_cell 您是否只需要注册它们,或者您是否必须包含if语句并根据某个变量更改布局以实现此效果. 例如: if cell.ad == true {} 原因是,我想要一个略有不同大小的单元格来存在
对于UICollectionViews,是否可以有多个单元格类型?

例如:

media_cell

regular_cell

ad_cell

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

例如:

if cell.ad == true {

}

原因是,我想要一个略有不同大小的单元格来存在图像.我想重新调整单元格可以工作,但我没有在网上看到任何东西.

有什么建议

解决方法

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

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

然后:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读