代码之家  ›  专栏  ›  技术社区  ›  Kirill

uiCollectionView中有多个不同的AdMob横幅广告

  •  1
  • Kirill  · 技术社区  · 7 年前

    我有一个 CollectionView 它有一个无限的卷轴,我正在努力使它,以便每9个细胞有一个AdMob横幅广告,而不是常规内容。

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

    是否可以显示 不同的 每隔9个手机打广告?

    这就是我现在的代码。它可以工作,但它会一遍又一遍地显示同一个广告:

    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 = "ca-app-pub-3940256099942544/2934735716"
               bannerView.delegate = self
               bannerView.rootViewController = self
               bannerView.translatesAutoresizingMaskIntoConstraints = false
    
               let request = GADRequest()
               bannerView.load(request)
    
               cell.contentView.addSubview(bannerView)
    
               return cell
    
            } else {
               // ...
            }
    }
    

    谢谢您!

    1 回复  |  直到 7 年前
        1
  •  1
  •   singh.jitendra    7 年前
    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 
            }
    }