Tag: tableview

updateSearchResults function iOS search in tableview by searchController

 guard let searchText = searchController.searchBar.text?.lowercased() else { return }
          DBReference.queryOrdered(byChild: "name").queryStarting(atValue: searchText).queryEnding(atValue: searchText+"\u{f8ff}")
            .observe(.value, with: {
        snapshot in
        var newItems: [WordsItem] = []
        for item in snapshot.children {
          let groceryItem = GroceryItem(snapshot: item as! DataSnapshot)
          newItems.append(WordsItem)
        }
        self.items = newItems
        self.tableView.reloadData()
      })

{ Add a Comment }

How to add Google Admob banner to the first row in UItableview, Swift IOS

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
        }

{ Add a Comment }