2017-05-03 45 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