单元格之间的随机广告

Random Ads between cells

我正在尝试将广告 完全随机 放置在 UITableView 内的单元格之间。我将向您展示我的主要文件,以了解我在做什么以及我想要的方式:

Table 视图控制器:


class Page1: UITableViewController, UISearchBarDelegate {
    
    @IBOutlet weak var searchBar: UISearchBar!
    var employeesSearching = [Employee]()
    var isSearching : Bool = false
    
    @IBOutlet weak var GoogleBannerView: GADBannerView!

    let collation = UILocalizedIndexedCollation.current()
    var sections: [[Any]] = []
    var objects: [Any] = [] {
        didSet {
            let selector: Selector = #selector(getter: UIApplicationShortcutItem.localizedTitle)
            sections = Array(repeating: [], count: collation.sectionTitles.count)
            let sortedObjects = collation.sortedArray(from: objects, collationStringSelector: selector)
            for object in sortedObjects {
                let sectionNumber = collation.section(for: object, collationStringSelector: selector)
                sections[sectionNumber].append(object as AnyObject)
            }
            self.tableView.reloadData()
        }
    }
    
    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
        }
    }
    
    func getMatches(letter: String, withArray array: [Employee]) -> [Employee] {
        return array.filter({ ([=10=].name.compare(letter, options: .diacriticInsensitive, range: [=10=].name.startIndex..<[=10=].name.index([=10=].name.startIndex, offsetBy: 1), locale: nil) == .orderedSame)})
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        if isSearching { return 1 }
        return collation.sectionTitles.count
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let letter = collation.sectionTitles[section]
        if isSearching {
            return employeesSearching.count
        } else {
            let matches = getMatches(letter: letter, withArray: Shared.instance.employees)
            if !matches.isEmpty { return matches.count }
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if isSearching { return nil }
        let letter = collation.sectionTitles[section]
        let matches = getMatches(letter: letter, withArray: Shared.instance.employees)
        if matches.count == 0 { return nil }
        return collation.sectionTitles[section] }
    
    override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        if isSearching { return nil }
        return collation.sectionIndexTitles }
    
    override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
        return collation.section(forSectionIndexTitle: index) }
    
    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
    }
    ...
}

如何将 UITableViewCell 走私为! AdCell随机进入UITableView?

我的意思是,我应该在 cellForRowAt 中做什么?我对所有这些索引部分有点困惑。

一种方法是在数据模型中的所需位置插入某种 "ad" 对象。

然后更新 cellForRowAt 以查看给定索引路径的数据模型中的对象。如果它是一个 "ad" 对象,创建并设置一个 "ad" 单元格。否则像现在一样创建和设置适当的数据单元格。

首先你需要生成一个介于 0 和你的 tableView 数据源数组大小之间的随机数

let lower : UInt32 = 0
let upper : UInt32 = array.count
let randomIndex = arc4random_uniform(upper - lower) + lower

那么您需要在数组中的随机索引处添加 Ad 对象

array.insert(AdObject, atIndex:randomIndex)

然后只需重新加载您的 tableView 并处理 cellForRow 函数中的不同类型