2017-11-30 3 views
0

私は、n番目(3番目)のセルごとに異なるセル(Ad)を表示しようとしています。このよう :スウィフトUITableView異なるN番目のセル

"Request" 
"Request" 
"Request" 
"Ad" 
"Request" 
"Request" 
"Request" 
"Ad" 
"Request" 
"Request" 
"Request" 
"Ad" 
"Request" 
... 

マイコード:

ものが私の変数

var requests = [RequestListable]() 
var list = [Listable]() 
var users = [UserInfo?]() 
var adInterval = 3 

私は広告をロードしようとしている私の機能だ

です。問題は通常、すべてのリクエスト(requestsArray)には1つの関連付けられたユーザー(usersArray)があり、「listArray」に広告を挿入するときに、ユーザーはもうリクエストと一致しないということです。どういたしまして。 そのため、広告が挿入されている同じインデックスにfakeUserを挿入していますが、それは機能しません。

私は私の 要求

func loadAds() 
    { 
     Api.adApi.observeAds 
     { 
      (ad, user) in 
      self.list = self.requests 
      for i in stride(from: self.adInterval, to: self.requests.count, by: self.adInterval).reversed() 
      { 
       self.list.insert(ad, at: i) 
       self.users.insert(user, at: i) // This is probably where i'ts going wrong 
       print(self.users.count) // Returns 40 (Don't know why) 
       print(self.list.count) // Returns 13 
      } 
     } 
    } 

ものは、私のテーブルビュー機能です

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    { 
     return list.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let listing = list[indexPath.row] 

     if let request = listing as? RequestListable 
     { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "RequestCell", for: indexPath) as! RequestTVCell 
      let user = users[indexPath.row] 
      cell.request = request 
      cell.user = user 
      cell.delegate = self 
      return cell 
     } 
     else if let ad = listing as? AdListable 
     { 
      let cell = tableView.dequeueReusableCell(withIdentifier: "AdCell", for: indexPath) as! AdTVCell 
      cell.ad = ad 
      cell.user = users[indexPath.row] 
      return cell 
     } 

     fatalError("Did not find data or ad for cell: Should never get here") 
    } 

だから問題があるフェッチ異なる機能、でテーブルビューをリロードしています:

人のユーザーが広告とユーザー

任意の提案を挿入 た後、それに関連する要求に合致していませんか?私は本当にそれを感謝します。

答えて

0

常にself.usersに挿入しているときは、self.requestsのコピーでobserveAds閉鎖が実行されるたびにself.listを交換しています。

0

あなたはrequestsよりusers多くを持っていて、userrequestに一致するように配列のインデックスを使用していることから、これらがアップし、一致していないように見えます。あなたはコメント'Returns 40 (Dont know why)を持っています - これを理解し、usersrequestsと一致していることを確認する必要があります。また、より密接にあなたのusersrequestsを結合することを検討したいかもしれません。それらを別々の配列に格納するのではなく、listのタプルタイプを使用し、requestの正しいuserであることを確認するメソッドを追加すると、requestの場合にはuserの権利があることがわかります。

var list = [(UserInfo, Listable)]()

関連する問題