2017-05-03 46 views
-1

我试图在UITableView之间的单元格之间将广告完全随机地放在之间。我要告诉我的主要文件,你明白我在做什么,我怎么想: Randomly Ads单元格之间的随机广告

表视图控制器

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({ ($0.name.compare(letter, options: .diacriticInsensitive, range: $0.name.startIndex..<$0.name.index($0.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?所有这些索引部分之间我都有点困惑。

+2

显示你的所有文件通常不会帮助人们理解。相反,它会迫使他们通读一段文字,以便找出可能导致错误的原因。一般来说,你应该试着弄清楚自己哪些是你的代码的相关部分,并只添加那些你的问题。请参见[如何创建最小,完整和可验证的示例](https://stackoverflow.com/help/mcve)。 –

+0

谢谢@PedroCastilho,我刚刚编辑了我的问题并简化了它! :) –

+1

您还没有发布任何问题。您已发布需求声明和一些代码。请[编辑]你的“问题”,这是一个真正的问题。你到底需要什么帮助?您发布的代码有哪些问题? – rmaddy

回答

1

首先,你需要为0,你的tableView数据源数组大小之间产生一个随机数

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

,那么你需要在randomIndex添加广告对象,数组中

array.insert(AdObject, atIndex:randomIndex) 

然后就重装你的tableView并处理cellForRow函数中的不同类型

+0

谢谢,现在介绍cellForRowAt中的广告。当我把'@IBOutlet weak var GoogleBannerView:GADBannerView!'和链接到'Main.Storyboard'时,我有一个错误。我刚刚更新了你的问题。 –

1

一种方法是在数据模型中的所需位置插入某种“广告”对象。

然后更新cellForRowAt以查看数据模型中对于给定索引路径的对象。如果它是“广告”对象,请创建并设置“广告”单元格。否则,请按照现在的方法创建并设置适当的数据单元格。

+0

真的吗?我知道,我提出了一个明确的问题,并且像你们问的那样两次编辑。我想知道我必须在'tableView.dequeueReusableCellWithIdentifier'上做些什么。 [请阅读](http://stackoverflow.com/help/how-to-answer) –

+0

我刚刚告诉你该怎么做。我的答案的哪一部分你不明白? – rmaddy