6

我已经在 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
4

3 回答 3

1

我建议您按照此处的步骤操作。他告诉我在细胞之间放置一个 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 不兼容。

于 2018-07-04T11:56:12.897 回答
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
于 2018-08-21T17:41:35.553 回答
0

首先在你的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)

希望对您有所帮助。

于 2018-07-01T14:08:40.050 回答