UICollectionView中有多个不同的Admob横幅广告

时间:2018-05-23 03:07:25

标签: ios swift uicollectionview admob ads

我有一个.Cells(lRow, 1) = wbk.Name .Cells(lRow, 2) = wks.Name .Cells(lRow, 3) = rFound.Address .Cells(lRow, 4) = rFound.Offset(0, 1).Value ,它有一个无限卷轴,我试图让它成为每个第9个单元格的Admob横幅广告,而不是常规内容。

我想确保每个广告都与前一个广告不同。但是,据我所知,这意味着每个新广告都需要新的CollectionView我必须通过Admob网站手动创建。

是否可以每9个单元格显示不同的广告?

这是我目前拥有的代码。它可以工作,但它一遍又一遍地显示相同的广告:

adUnitID

谢谢!

1 个答案:

答案 0 :(得分:1)

var arrAdUnitID = [String array of all Ad Unit ID]
var currentIndexForAdd = 0
var currentIndexForNormalRow = 0


override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

       if (indexPath.item % 9 == 0) {

           let cell = collectionView.dequeueReusableCell(withReuseIdentifier: adCellId, for: indexPath) as! AdCell

           let adSize = GADAdSizeFromCGSize(CGSize(width: self.view.frame.width, height: adViewHeight))
           let bannerView = GADBannerView(adSize: adSize)

           bannerView.adUnitID = arrAdUnitID[currentIndexForAdd]
           currentIndexForAdd += 1
           bannerView.delegate = self
           bannerView.rootViewController = self
           bannerView.translatesAutoresizingMaskIntoConstraints = false

           let request = GADRequest()
           bannerView.load(request)

           cell.contentView.addSubview(bannerView)

           return cell

        } else {
           cell.title.text = arrDataToShow[currentIndexForNormalRow] // It can be any thing like image or text which you want to show
           currentIndexForNormalRow += 1 
        }
}