我的目标是在我的UITableView中显示使用Sections的Native Express Ad。每10个TOTAL单元格(将所有部分的单元格组合在一起)应该是Native Express Ad。每个其他细胞都是正常细胞。
问题是没有广告出现过。我相信问题出在我的cellForRow演员身上。我在这里跟随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。
提前感谢您提供的任何帮助!
答案 0 :(得分:0)
这是因为加载了第一个广告(来自您提供的视频链接)
func nativeExpressAdViewDidReceiveAd(_ nativeExpressAdView: GADNativeExpressAdView) { }
此外,使用指定的大小。
let size = GADAdSizeFullWidthPortraitWithHeight(300)
尝试在deque单元格中加载请求,因为广告加载发生在单元格双端队列中。不用担心,因为我们在添加之前删除子视图。
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
}