3

I have integrated AdMob standard banner into a static tableview cell (tableview : 1 section, 5 rows).

I would like to automatically hide or show the relevant tableview cell (that contains the AdMob banner view) depending on whether the banner is loaded or not.

My code for the AdMob banner integration:

import UIKit
import GoogleMobileAds

class WorkoutsFreeListTVC: UITableViewController, GADBannerViewDelegate {

@IBOutlet weak var bannerView: GADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()

    bannerView.hidden = true
    bannerView.adUnitID = "ca-app-pub-9451126739340372/2084650447"
    bannerView.rootViewController = self
    bannerView.delegate = self

    let request = GADRequest()
    request.testDevices = ["dcf33ab873fb56fdf5ff94bd7a5f3fde"]

    bannerView.loadRequest(GADRequest())
}

Functions to test if banner loads or otherwise:

func adViewDidReceiveAd(bannerView: GADBannerView!) {
    bannerView.hidden = false
}

func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
    bannerView.hidden = true
}

My bannerView will always be presented (if available for load) in the same tableview cell (row index 3).

I am thinking along the lines of this code to achieve my desired result:

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 3 && !myImageIsLoaded {
    return 0
}
// Will hide just the fourth row of my table if myImageIsLoaded is false

return 50

}

which I got from a similar question whereby cell height was returned as zero (0) and effectively "hidden" if !myImageIsLoaded.

I have been trying to amend the code to my needs, but am stuck.

Questions: Am I on the right track? Is this how I should approach the issue? How can I amend the code to my needs?

Thank you for your time ;)

1 Answers1

2

Katherine Jenkins,

Yes the approach you are following is absolutely fine.

In your case probably all you want to check is bannerView is hidden or not. If hidden then set the row height to 0 else return the height of the cell properly.

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 3 {
        if bannerView.hidden
             return 0
        else 
             return 50 //value of height for banner cell
    }
    else {
        return 50 //return other cell height
    }
}

and in order to reflect the changes in the cell height you can do

func adViewDidReceiveAd(bannerView: GADBannerView!) {
    bannerView.hidden = false
    self.tableView.reloadData()
}

func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
    bannerView.hidden = true
    self.tableView.reloadData()
}
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • Thanks Sandeep. Thanks for filling in the blanks for me. Will also incorporate `self.tableView.reloadData()` as you advised. Brilliant! Have a wonderful da! – Katherine Jenkins May 10 '16 at 10:20