Ios UICollectionView每x个单元格查看不同的单元格

Ios UICollectionView每x个单元格查看不同的单元格,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,所以我想在UIcollectionView中添加广告。在每4个文件夹之后,我想显示一个广告(总共3次,因此第4、9和14行) 我尝试了以下方法: override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if indexPath.row == 4 || indexPath.ro

所以我想在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次广告,但现在不再显示该indexath.row的我的文件夹(4、9和14)。有没有办法在collectionview和文件夹中添加我的广告


谢谢

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

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!
    }
}
编辑

更全面的方法是修改
文件夹
数组(?),以支持
AdUnitsCollectionViewCell
FolderViewCell
数据。
例如,如果文件夹是一个结构数组,则

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

需要有一种方法,该方法每4次将ad注入
文件夹
数组,然后您可以打开
文件夹[indexath.row]。键入
以决定是显示ad单元格还是显示文件夹单元格。您也不需要修改
numberofitemsinssection
方法。

如果您想在集合视图(将是total\u folder+total\u 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。希望它有帮助

您需要在TableViewDataSources的
numberOfRowsInSection
方法中添加+3也请参见@reinierMelian希望这很简单。给我一个索引超出范围(必须小于187)”,因为在indexath.row上我只放置一个单元格,而不放置另一个单元格。这可能会导致索引超出文件夹上集合的大小。仅仅添加行数是不够的,然后在配置实际单元格时需要考虑负偏移量。否则,您只是跳过文件夹集合中的行,最终将超出文件夹的边界collection@bhmahler我们不能指望为他写整个班级。他也没有分享
文件夹
数据的样子。我认为这有助于让他朝着正确的方向前进。这当然会导致文件夹超出范围!