所以我想在我的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和我的文件夾中?
謝謝!
您需要在您的tableViewDataSource –
又見你的'numberOfRowsInSection'方法添加+3 [如何集合視圖中顯示廣告(http://stackoverflow.com/q/43062278/643383) – Caleb
@ReinierMelianwish就這麼簡單。給我一個索引是越界(必須小於187)',因爲在indexpath.row我只放置一個單元格,但不是其他 – SoundShock