问题
I've been trying to implement Admob on swift and everything is good, but i run out of problem because I'm using custom table view cell and "Table view controller" as the main swift file, so when i am dragging the BannerView into the storyboard which is the table view controller, the ad actually appear in the first cell of the table view ! instead i need to load it at the button of the tableview controller not inside the cells ?
@IBOutlet weak var bannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
self.bannerView.rootViewController = self
var request: GADRequest = GADRequest()
request.testDevices = [GAD_SIMULATOR_ID]
self.bannerView.loadRequest(request)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
回答1:
One solution could be to create a new instance of bannerView
instead of having it as an outlet and then add it as a subview to the tableView's superview.
回答2:
I had the same issue , i got it figured out by a slightly different method, But it works like a charm. So for all those Who are new to Swift and face this problem,
If Your using a story board, copy all your views( ex labels, UIImage's ) from the prototype cell
Go ahead and delete your View Controller. ( fear not , it will work eventually , trust me )
Drag a blank ViewController and in the class , mention your class associated with earlier tableView.
drag also tableview on it and selecting the tableview, put constraints form storyboard up & down-> 0 & left & right -> -20 .
drag a tableview cell, and selecting that cell, in class mention the cell class you were using for the earlier Tableview cell, (tip-> You jus mite want to click on the module bellow the class and press enter, i got a stupid bug { unknown class in interface builder error , when i did it } )
now you can just select the cell and paste your earlier views in it and create a new connection with the cell class @IB outlets.
In the same Viewcontroller you can drag an empty UIView and make it a GADBannerView ( follow the Googles guide for admob iOS) Thats all in the IB
Now go to your tableview class , the class extension just make it to UIViewController , and confirm to protocols UITableViewDataSource & UITableViewDelegate.
ex: class Foo: UIViewController, UITableViewDataSource, UITableViewDelegate {
- make a IB connection of your newly added UITableview in your main tableview (now viewController) class @IBOutlet weak var Footableview: UITableView!
Now jus add these two lines in the viewDidLoad Footableview.datasource = self Footableview.delegate = self
- Now just remove override form all tableview functions such as sections or RowForIndex functions, and wherever there is 'self.tableView' replace with 'self.Footableview'.
ScreenShot
- Regards
来源:https://stackoverflow.com/questions/28797081/place-admob-ad-at-the-bottom-of-tableview-controller-swift