【问题标题】:Facebook native Ads are not clickable again in UITableView Cells after scroll滚动后在 UITableView 单元格中无法再次点击 Facebook 原生广告
【发布时间】:2018-06-09 05:21:30
【问题描述】:

我已经在 UITableView 中实现了 facebook 原生广告,前 1-2 次它可以点击,但是当我滚动 tableview 并再次回到同一个单元格时,现在广告没有点击,我使用的是 swift 3.2 下面是单元实现。

let ad = adsManager.nextNativeAd
let cell = self.tableHome.dequeueReusableCell(withIdentifier: "HomeAdsTableViewCell", for: indexPath) as! HomeAdsTableViewCell
cell.message.text = ad?.body
cell.title.text = ad?.title
cell.callToActionButton.setTitle(ad?.callToAction, for: .normal)

if let pic = ad?.coverImage {
    cell.postImage.setImageWithIndicator(imageUrl:pic.url.absoluteString)
}

ad?.registerView(forInteraction: cell.postView, with: self)
cell.selectionStyle=UITableViewCellSelectionStyle.none

return cell

【问题讨论】:

    标签: ios swift facebook uitableview native-ads


    【解决方案1】:
    //create a new object  of nativead in your class//
    
    var previousNativead : FBNativeAd?
    
    let ad = adsManager.nextNativeAd
    self.previousNativead?.unregisterView()
    let cell = self.tableHome.dequeueReusableCell(withIdentifier: "HomeAdsTableViewCell", for: indexPath) as! HomeAdsTableViewCell
    cell.message.text = ad?.body
    cell.title.text = ad?.title
    cell.callToActionButton.setTitle(ad?.callToAction, for: .normal)
    
    if let pic = ad?.coverImage {
        cell.postImage.setImageWithIndicator(imageUrl:pic.url.absoluteString)
    }
    previousNativead = ad
    ad?.registerView(forInteraction: cell.postView, with: self)
    cell.selectionStyle=UITableViewCellSelectionStyle.none
    
    return cell
    

    【讨论】:

      【解决方案2】:

      我建议您按照此处的步骤操作。他告诉我在单元格之间放置一个 facebook 广告。

      https://www.appcoda.com/facebook-ads-integration/

      这就像你在你变成白痴的地方不会犯错误。

      您应该将链接中的这一部分改编为您自己的。

          func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
             if adsCellProvider != nil && adsCellProvider.isAdCellAtIndexPath(indexPath, forStride: UInt(adRowStep)) {
               return adsCellProvider.tableView(tableView, cellForRowAtIndexPath: indexPath)
             }
             else {
               let cell = tableView.dequeueReusableCellWithIdentifier("idCellSample", forIndexPath: indexPath) as! SampleCell
               cell.lblTitle.text = sampleData[indexPath.row - Int(indexPath.row / adRowStep)]
               return cell
             }  }
      

      您也应该在屏幕刷新和转动时使用此方法。

          func configureAdManagerAndLoadAds() {
            if adsManager == nil {
              adsManager = FBNativeAdsManager(placementID: "PLACEMENT_ID", forNumAdsRequested: 5)
              adsManager.delegate = self
              adsManager.loadAds()
            }}
          override func viewWillAppear(animated: Bool) {
            configureAdManagerAndLoadAds()}
      

      最后你应该检查相关字段,这可能与 content swift 3 不兼容。

      【讨论】:

      • 我正在使用自定义单元格显示广告,您的代码用于默认单元格。
      【解决方案3】:

      首先在您的 viewDidLoad() 中检查您是否添加了

      override func viewDidLoad() {
           super.viewDidLoad()
           adsManager = FBNativeAdsManager(placementID: "YOUR_PLACEMENT_ID_HERE", forNumAdsRequested: "YourNumAds")
           adsManager.delegate = self
           adsManager.loadAds()
      }
      

      那么为了遵守 FBNativeAdsManagerDelegate,您需要添加以下方法,nativeAdsLoaded()。

          func nativeAdsLoaded() { 
          adsCellProvider = FBNativeAdTableViewCellProvider(manager: adsManager, forType: FBNativeAdViewType.genericHeight300)
          adsCellProvider.delegate = self
          if self.tableview != nil {
               self.tableview.reloadData()
             }
           }
      

      您应该做的最后一件事是直接 registerView 到单元格而不是 postView

      ad?.registerView(forInteraction: cell, with: self)
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-03
        • 1970-01-01
        • 1970-01-01
        • 2018-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多