2017-02-03 62 views
0

我的目標是讓Native Express廣告顯示在使用區段的UITableView中。每10個TOTAL細胞(將所有部分的細胞組合在一起)應該是Native Express Ad。每個其他細胞將是一個正常的細胞。本機特快廣告未在我的UITableView中顯示

問題是沒有廣告顯示出來。我相信問題在於我的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。

非常感謝您提供任何幫助!

回答

0

這是由於裝載的第一個廣告(根據您提供的視頻鏈接)

func nativeExpressAdViewDidReceiveAd(_ nativeExpressAdView: GADNativeExpressAdView) { } 

而且,指定的大小,使用。

let size = GADAdSizeFullWidthPortraitWithHeight(300) 

嘗試在deque單元中加載請求,因爲廣告加載發生在cell 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 

}