2017-02-03 15 views
0

私の目標は、セクションを使用しているUITableViewにNative Express広告を表示することです。 10th TOTALセル(すべてのセクションのセルを組み合わせる)は、ネイティブ・エクスペリエンス広告でなければなりません。他のすべてのセルは通常のセルになります。Native Express広告がUITableView内に表示されない

問題はありません。広告は表示されません。問題は私のcellForRowAtキャストにあると思う。私はAndrew Brogdonのチュートリアルに続きました。https://www.youtube.com/watch?v=chNb7-k6m4M - 私が混乱している部分は5:10マークの周りにあります。

私は私のコード細胞が両方FruitObjectsを保持することができるようにANYOBJECTとしてobjectsArrayをキャストし、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) { } 

答えて

0

これは(あなたが提供されるビデオのリンクから)の最初の広告をロードによるものである、指定されたサイズ、使用。

let size = GADAdSizeFullWidthPortraitWithHeight(300) 

デキューセルで要求をロードしようとすると、広告ロードがセルデキューで行われるためです。追加する前にサブビューを削除しているので心配はありません。

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 

}

関連する問題