繁体   English   中英

UICollection每x个单元格查看一个不同的单元格

[英]UICollectionView different cell every x cells

所以我想在UIcollectionView内添加广告。 在第4个文件夹之后,我想展示一个广告(共3次,因此第4,9和14行)。

我尝试了以下方法:

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.row == 4 || indexPath.row == 9 || indexPath.row == 14 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdUnitsCollectionViewCell", for: indexPath) as? AdUnitsCollectionViewCell
            //cell?.addSubview(AdUnitController().getBannerView(2))
            //cell?.view = AdUnitController().getBannerView(2)
            cell?.view.adUnitID = "/6499/example/banner"
            cell?.view.rootViewController = self
            cell?.view.load(DFPRequest())
            return cell!
        } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell
        cell!.configure(folders![indexPath.row])
        return cell!
        }
    }

确实确实在每个第4个文件夹之后3次显示广告,但是现在我的indexPath.row文件夹不再显示(4、9和14)。 有什么方法可以将广告添加到collectionview和文件夹中吗?

谢谢!

首先,如果要在集合视图中添加项目/行,则必须增加项目/行的总数(将为total_folder + total_add),但正如@bhmahler所说:仅添加行数是不够的,那么您需要抵消负偏移

您可以做的是,您可以记录它进入if块的时间 ,如果它进入此块,可以增加计数

if indexPath.row == 4 || indexPath.row == 9 || indexPath.row == 14 {
   count+=1 //which is initially set to zero 
}

现在,在您的else块中,您只需减去索引路径中的计数即可,如下所示

else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell
        cell!.configure(folders![indexPath.row-count])
        return cell!
    }

确保在集合视图委托方法之外设置count = 0

对不完整的代码感到抱歉,因为我不知道swift.Hope它有帮助

如果要同时显示广告文件夹,则需要增加行数

override func numberOfItems(inSection section: Int) -> Int {
   let count = //the count you were using before
   return count + floor(count/4) //add an ad every 4
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.row % 4 == 0 && indexPath.row > 0 {  //assuming you don't want an ad as the first item
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdUnitsCollectionViewCell", for: indexPath) as? AdUnitsCollectionViewCell
        //cell?.addSubview(AdUnitController().getBannerView(2))
        //cell?.view = AdUnitController().getBannerView(2)
        cell?.view.adUnitID = "/6499/example/banner"
        cell?.view.rootViewController = self
        cell?.view.load(DFPRequest())
        return cell!
    } else {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell
        cell!.configure(folders![indexPath.row])
        return cell!
    }
}

编辑

一种更全面的方法是修改folders数组(?),以同时支持AdUnitsCollectionViewCellFolderViewCell数据。
例如,如果folders是结构数组,则为

struct CellData {
    static enum CellDataType {
        case folder
        case ad
    }
    let type: CellDataType
    let data: //some type - you haven't shown whats required
}

将需要一种方法,每隔4个将广告注入folders数组,然后您可以打开folders[indexPath.row].type来决定要显示广告单元格还是文件夹单元格。 您也不需要修改numberOfItemsInSection方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM