ios - 如何在 uitableview admob swift 中显示广告?

标签 ios swift xcode admob tableview

我对 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
}

提前谢谢您

最佳答案

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

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
    }
}

谢谢

关于ios - 如何在 uitableview admob swift 中显示广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65748200/

相关文章:

ios - Swift - 如何创建正确的 sectionForSectionIndexTitle?

ios - Swift 4-应用程序在按下启动 segue 的按钮时崩溃

objective-c - 如何在 Objective-C 中使用 Swift 枚举?

ios - 将 Objective-C 桥接到 Swift 项目

ios - UITableView 在底部加载更多单元格,多次调用方法

ios - 用条纹填充 UIBezierPath 形状

swift - 对象内部对象的 Realm 谓词

ios - 返回两种不同类型的 Swift 函数

ios - 使用 Xcode 6 在所有设备上查看 3,5"显示器

Xcode 4 : Update CFBundleVersion on each build using Git repo commit version