2
I’m trying to insert Ads totally random between cells within a UITableView. That’s kind of what I want:
So is mine Table View Controller:
import UIKit
import GoogleMobileAds
class Page1: UITableViewController, UISearchBarDelegate, GADBannerViewDelegate {
...
...
...
@IBOutlet weak var GoogleBannerView: GADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.delegate = self
self.tableView.contentOffset = CGPoint(x: 0, y: searchBar.frame.height) //hide searchBar
Shared.instance.employees.sort {
(first, second) in
first.name.compare(second.name, options: .diacriticInsensitive) == .orderedAscending
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 3 || indexPath.row == 9 || indexPath.row == 14 {
let cellAd = tableView.dequeueReusableCell(withIdentifier: "cellAd", for: indexPath)
GoogleBannerView?.adUnitID = "ca-app-pub-6043248661561548/4628935113"
GoogleBannerView?.rootViewController = self
GoogleBannerView?.load(GADRequest())
return cellAd
}
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell1
if isSearching {
cell.nameLabel.text = employeesSearching[indexPath.row].name
cell.positionLabel.text = employeesSearching[indexPath.row].position
} else {
let letter = collation.sectionTitles[indexPath.section]
let matches = getMatches(letter: letter, withArray: Shared.instance.employees)
cell.nameLabel.text = matches[indexPath.row].name
cell.positionLabel.text = matches[indexPath.row].position
}
return cell
}
...
...
...
}
I wish someone could explain to me what I should do at cellForRowAt so that I could add the Ads. Because in the middle of the Indexed sections I’m kind of confused.
EDIT
This is my Tableviewcell class currently:
import UIKit
class TableViewCell1: UITableViewCell {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var positionLabel: UILabel!
}

Creates another cell with another Identifier pro ad. Returns one extra cell per session. Then checks if the
indexPath.row == 0and if it is use add cell instead of the other.– dbmrq
I was able to display the cells the way I wanted. But I can’t insert a
GADBannerViewthen... I don’t know where I’m going wrong. I edited my question for you to see how it turned out. I can’t associate the@IBOutlet GoogleBannerViewto the Main.Storyboard.– Arnon
You have to create a subclass of
UITableViewCell, use this class for the ad cell and this is where you will add the outlet and associate to the storyboard.– dbmrq
Excuse the amateurism, but how do I create this subclass? I Googled but found nothing useful. I also edited my question to show you the current structure of
TableViewCell.– Arnon
Dude, you’ll have to look for tutorials on how to use Uitableview and train a little. That’s the basics, and it’s a lot for me to explain here.
– dbmrq
Don’t worry, you’ve helped me a lot! I think I got it here. Fight
– Arnon
Good luck out there. :)
– dbmrq