step 1: create file AdCell.swift


import UIKit
import GoogleMobileAds

class AdCell: UITableViewCell {
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = .clear
    }
    
    class func cellBannerView(rootVC: UIViewController, frame: CGRect) -> GADBannerView {
           let bannerView = GADBannerView()
           bannerView.frame = frame
           bannerView.rootViewController = rootVC
           bannerView.adUnitID = "ca-app-pub-3940256099942544/6300978111"
        bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(frame.size.width)
           return bannerView
       }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}

step 2: use the Adcell class:


 let bannerViewHeight = GADBannerView(adSize: kGADAdSizeBanner).frame.size.height



 override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
             return bannerViewHeight + 15
    }

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
             let cell = AdCell(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: bannerViewHeight))                     
             let bannerView = AdCell.cellBannerView(rootVC: self, frame:cell.bounds)
             bannerView.translatesAutoresizingMaskIntoConstraints = false
             cell.addSubview(bannerView)
                                
             bannerView.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
             bannerView.centerYAnchor.constraint(equalTo: cell.centerYAnchor, constant: -3).isActive = true

             let request = GADRequest()
             bannerView.load(request)

             return cell
        }