2017-04-04 118 views
0

所以我想在我的UIcollectionView中添加广告。每隔4个文件夹后,我想显示一个广告(总共3次,因此第4,9和14行)。UICollectionView不同的单元格每个x单元格

我已经试过如下:

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

但这3次的确展示广告的每4个文件夹后,但现在我为indexPath.row文件夹不再显示(4,9和14)。有没有一种方法可以将我的广告添加到collectionview和我的文件夹中?

谢谢!

+0

您需要在您的tableViewDataSource –

+0

又见你的'numberOfRowsInSection'方法添加+3 [如何集合视图中显示广告(http://stackoverflow.com/q/43062278/643383) – Caleb

+0

@ReinierMelianwish就这么简单。给我一个索引是越界(必须小于187)',因为在indexpath.row我只放置一个单元格,但不是其他 – SoundShock

回答

0

您将需要增加的行数,如果你想要同时显示广告文件夹

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方法。

+2

这可能会导致索引超出文件夹集合的大小。只需添加行数是不够的,然后在配置实际单元时需要考虑负偏移量。否则,你只是跳过文件夹集合中的行,最终会超出文件夹集合的边界 – bhmahler

+0

@bhmahler我们不能期望为他写整个类。他还没有分享“文件夹”数据的外观。我认为这有助于让他走向正确的方向。 – dmorrow

1

你必须要增加项目的总数量/如果你想添加它集合视图里面排第一(这将是total_folder + total_add),但作为@bhmahler说:刚添加的行数不够了,你再需要考虑的负偏移

你可以做什么是你可以保持它有多少时间去了内如果块,如果走到该块内增加了计数,计数

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

现在在你的其他区块你可以简单地减去指数路径计数,这样

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

务必将数= 0外的集合视图的委托方法

对不起,不完整的代码,我不知道SWIFT 。希望它有助于

相关问题