gpt4 book ai didi

ios - Native Express 广告未在我的 UITableView 中显示

转载 作者:行者123 更新时间:2023-11-30 12:42:52 24 4
gpt4 key购买 nike

我的目标是让 Native Express 广告出现在我使用部分的 UITableView 中。每 10 个 TOTAL 单元格(将所有部分的单元格组合在一起)应该是一个 Native Express 广告。其他所有细胞都是正常细胞。

问题是没有广告出现。我相信问题出在我的 cellForRowAt 类型转换上。我在这里遵循了 Andrew Brogdon 的教程:https://www.youtube.com/watch?v=chNb7-k6m4M - 我认为我感到困惑的部分是在 5:10 左右。

我将 objectsArray 转换为 AnyObject,以便单元格可以同时容纳 FruitObjects 和 GADNativeExpressAdView

我的代码:

类别:

struct FruitObjects {
var sectionName: String!
var sectionObjects: [FruitModel]!
}
var objectsArray = [AnyObject]()

viewDidLoad:

objectsArray = [
FruitObjects(sectionName: "A", sectionObjects: [
FruitModel(fruitTerm: "Apple", definition: "Lorem Ipsum Dolor Sit Amet."),
FruitModel(fruitTerm: "Apple 2", definition: "Lorem Ipsum Dolor Sit Amet."),
FruitModel(fruitTerm: "Apple 3", definition: "Lorem Ipsum Dolor Sit Amet.")
]) as AnyObject,

FruitObjects(sectionName: "B", sectionObjects: [
FruitModel(fruitTerm: "Banana", definition: "Lorem Ipsum Dolor Sit Amet."),
FruitModel(fruitTerm: "Banana 2", definition: "Lorem Ipsum Dolor Sit Amet."),
FruitModel(fruitTerm: "Banana 3", definition: "Lorem Ipsum Dolor Sit Amet.")
]) as AnyObject,

FruitObjects(sectionName: "C", sectionObjects: [
FruitModel(fruitTerm: "Clementine", definition: "Lorem Ipsum Dolor Sit Amet."),
FruitModel(fruitTerm: "Clementine 2", definition: "Lorem Ipsum Dolor Sit Amet.")
]) as AnyObject

]

UITableViewDataSource:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let myCast = objectsArray[section] as? FruitObjects
return myCast!.sectionObjects.count
}

func numberOfSections(in tableView: UITableView) -> Int {
return objectsArray.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
let myCast = objectsArray[section] as? FruitObjects
return myCast!.sectionName
}

cellForRowAt:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let myCast = objectsArray[indexPath.section] as? FruitObjects{
let cell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath)
cell.textLabel?.text = myCast.sectionObjects[indexPath.row].fruitTerm
print("CELL")
return cell
}
else {
let adView = objectsArray[indexPath.row] as! GADNativeExpressAdView
let reusableAdCell = tableView.dequeueReusableCell(withIdentifier: "NativeExpressAdViewCellReuseID", for: indexPath)
// MISSING removeFromSuperview https://www.youtube.com/watch?v=chNb7-k6m4M 5:51
reusableAdCell.contentView.addSubview(adView)
adView.center = reusableAdCell.contentView.center
print("NATIVE")
}
}

您能帮助我让原生广告每 10 个单元格显示一次吗?

我认为问题出在我上面发布的代码中。也许在我的选角中?如果没有什么看起来不和谐的地方,请告诉我,我将发布更多关于我的 ViewController 的内容。

预先感谢您提供的任何帮助!

最佳答案

这是由于加载了第一个广告(来自您提供的视频链接)

 func nativeExpressAdViewDidReceiveAd(_ nativeExpressAdView: GADNativeExpressAdView) { }

此外,还指定了使用的大小。

let size = GADAdSizeFullWidthPortraitWithHeight(300)

尝试在双端队列单元格中加载请求,因为广告加载发生在双端队列单元格中。不用担心,因为我们会在添加之前删除 subview 。

   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  
let cell = tableView.dequeueReusableCell(withIdentifier: "NativeExpressAdViewCellReuseID",for: indexPath)

let adView = self. objectsArray[indexPath.row] as! GADNativeExpressAdView

for subView in cell.contentView.subviews {
subView.removeFromSuperview()
}

cell.selectionStyle = .none
cell.contentView.addSubview(adView)
let request = GADRequest()
request.testDevices = [kGADSimulatorID]//for running it in simulator
adView.load(request)
adView.center = cell.contentView.center
return cell

}

关于ios - Native Express 广告未在我的 UITableView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42034139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com