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