【问题标题】:How to display ad in uitableview admob swift?如何在uitableview admob swift中显示广告?
【发布时间】:2021-01-16 09:37:38
【问题描述】:

我对 admob nativeads 很陌生,我检查了每个教程 网上关于如何在tableview中添加广告,但我没有找到任何运气 这是迄今为止我在 cellforrowat 函数中的代码:

let cell = Bundle.main.loadNibNamed("NativeAdTableViewCell", owner: self, options: nil)?.first as! NativeAdTableViewCell
        
        print("ad = \(ad == nil)")
        cell.adTitle.text = ad?.headline
        cell.adSubtitle.text = ad?.body
        cell.img.image = ad?.icon?.image
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
        return cell

其他代码:

func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd) {
    nativeAd.delegate = self
    ad = nativeAd
}

在此感谢您

【问题讨论】:

    标签: ios swift xcode admob tableview


    【解决方案1】:

    当收到您的添加时,您可以简单地在 tableView 数据源数组中添加一个空元素,如下所示:

    func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADUnifiedNativeAd {
       nativeAd.delegate = self
       self.yourDataSource.insert("", at: 0) // add empty element in where you show your ads
       ad = nativeAd
       yourTableView.reloadData() 
    }
    

    之后你的 tableViewCell 看起来像:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let data = self.yourDataSource[indexPath.row]as? [String]
        if data == nil {
            
            let nativeAd = self.ad //Your native ads object
            nativeAd.rootViewController = self
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "nativecell", for: indexPath)as! nativecell // nativecell is your native ad cell
            let adView = cell.adview
            adView!.nativeAd = nativeAd
            cell.headline.text = nativeAd.headline
            cell.icon.image = nativeAd.icon?.image
            cell.body.text = nativeAd.body
            cell.action.isUserInteractionEnabled = false
            cell.action.setTitle(nativeAd.callToAction, for: UIControl.State.normal)
            return cell
            
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "regularCell", for: indexPath)as! regularCell
            
            return cell
        }
    }
    

    谢谢

    【讨论】:

    • 谢谢你的回答,我搞定了!
    • 等一下,我有一个小问题,显示了广告的信息,但是广告无法点击,我复制了你的代码,这是为什么?
    • 确保所有组件都在 tableViewCell 的 GADNativeUnifiedAdView 中
    • 有人有这个的 obj C 版本吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多