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 ;)