【问题标题】:How to resize second cell in collectionview Swift?如何在collectionview Swift中调整第二个单元格的大小?
【发布时间】:2021-01-21 09:18:04
【问题描述】:

我想在我的帖子之间添加原生广告,但问题是第二个单元格是“adcell”,使用与“cell”相同的高度,我该如何更改“asdcell”的高度?

这是我的 adcell 代码

 collectionView.register(UINib(nibName: "AdViewCell", bundle: nil) , forCellWithReuseIdentifier: "adcell")
           collectionView.delegate = self
           collectionView.dataSource = self
           view.addSubview(collectionView)

并展示广告

 let adcell = collectionView.dequeueReusableCell(withReuseIdentifier: "adcell", for: indexPath) as! AdViewCell
    
      if (indexPath.item % 5 == 1){
        
        //admob code here

    return adcell

此代码用于设置帖子的高度

     func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      if UIScreen.main.bounds.size.height > 960{ //Write iPhone or iPad size. If iPad :
         return CGSize(width: (((collectionView.frame.width) - 40) / 2), height: 500)
      } else { //if iPhone
           return CGSize(width: view.frame.width-20 , height: 500)
      }
  }

【问题讨论】:

    标签: ios swift uicollectionview cell


    【解决方案1】:

    如果您知道每五个单元格会添加一个单元格(顺便说一句,如果您想实现这一点,请将 indexPath.item % 5 == 1 更改为 indexPath.item % 5 == 0),那么在 sizeForItemAt 中您可以使用与 cellForItemAt 相同的逻辑:

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
       if indexPath.row % 5 == 0{ 
          //return size for addcell ehere
       }
    
       if UIScreen.main.bounds.size.height > 960{ //Write iPhone or iPad size. If iPad :
          return CGSize(width: (((collectionView.frame.width) - 40) / 2), height: 500)
       } else { //if iPhone
    
       return CGSize(width: view.frame.width-20 , height: 500)
    
       }
    
    }
    

    【讨论】:

    • 什么不起作用?请展示你是如何实现它的
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多