Random Ads Between Cells in a Table View

1

I'm trying to insert totally random ads between cells within a UITableView . It's more or less what I want:

Sothere'smyTableViewController:

importUIKitimportGoogleMobileAdsclassPage1:UITableViewController,UISearchBarDelegate,GADBannerViewDelegate{.........@IBOutletweakvarGoogleBannerView:GADBannerView!overridefuncviewDidLoad(){super.viewDidLoad()self.searchBar.delegate=selfself.tableView.contentOffset=CGPoint(x:0,y:searchBar.frame.height)//hidesearchBarShared.instance.employees.sort{(first,second)infirst.name.compare(second.name,options:.diacriticInsensitive)==.orderedAscending}}overridefunctableView(_tableView:UITableView,cellForRowAtindexPath:IndexPath)->UITableViewCell{ifindexPath.row==3||indexPath.row==9||indexPath.row==14{letcellAd=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 would like someone to explain to me what I should do in cellForRowAt for me to add Ads. Because between the indexed sections I'm kind of confused.

EDIT

This is my current TableViewCell class:

import UIKit

class TableViewCell1: UITableViewCell {

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var positionLabel: UILabel!

}
    
asked by anonymous 03.05.2017 / 20:54

1 answer

0

To insert Random ADs, I indicate that you modify the data array that you use to mount the tableview, before displaying the tableview.

For this, you could insert the ADS into your data array ... or within the tableView(tableView: UITableView, numberOfRowsInSection section: Int) method together with a isAdsAdicionadas variable; or in some method called by its code BEFORE the numberOfRowsInSection , at instantiation of the screen (I particularly prefer this approach)

So I saw the array you're using for this is matches

Then I could do more or less this:

Create attribute isAdsAdicionadas and numberOfRowsInSection check variable status, if false, call method inserirADSRandomicamente :

let isAdsAdicionadas: Bool = false

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if(isAdsAdicionadas == false){

        self.inserirADSRandomicamente()
    }

    isAdsAdicionadas = true

    return matches.count
}

func inserirADSRandomicamente(){

    var indiceRandomico: Int!
    var arrayIndiceRandomico:[Int] = []
    let numeroMaximoDeADS = 3
    var contador = 0

    func randomizar(){

        indiceRandomico = Int(arc4random_uniform(UInt32(matches.count)))
        if(contador <= numeroMaximoDeADS){
            if(arrayIndiceRandomico.contains(indiceRandomico) == true){
                randomizar()
            }
            else{
                let objetoADS = "OBJETO ADS"
                matches.insert(objetoADS, at: indiceRandomico)
                arrayIndiceRandomico.append(indiceRandomico)
                contador += 1
                randomizar()
            }
        }
        else{
            return
        }
    }

    randomizar()
}

From there, you already have the array with random ADSs. Just treat the cases of being ADS or not, in method func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

You can add more logic to the randomizar() method if you do not want to display two ADSs, for example.

    
03.08.2017 / 00:26