2017-04-04 18 views
0

私はUIcollectionView内に広告を追加したいと思います。 4番目のフォルダごとに、広告を表示したい(合計3回、したがって4,9と14行)。UICollection xセルごとに異なるセルを表示する

私は次のことを試してみた:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     if indexPath.row == 4 || indexPath.row == 9 || indexPath.row == 14 { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdUnitsCollectionViewCell", for: indexPath) as? AdUnitsCollectionViewCell 
      //cell?.addSubview(AdUnitController().getBannerView(2)) 
      //cell?.view = AdUnitController().getBannerView(2) 
      cell?.view.adUnitID = "/6499/example/banner" 
      cell?.view.rootViewController = self 
      cell?.view.load(DFPRequest()) 
      return cell! 
     } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell 
     cell!.configure(folders![indexPath.row]) 
     return cell! 
     } 
    } 

これは確かに3回4回ごとのフォルダの後に広告が表示されないが、今そのindexPath.rowのための私のフォルダはもはや示されていない(4、9、および14)。私はコレクションビューと私のフォルダの中に私の広告を追加する方法はありますか?

ありがとうございます!

+0

tableViewDataSourceの 'numberOfRowsInSection'メソッドに+3を追加する必要があります –

+0

参照[コレクションビュー内に広告を表示する方法](http://stackoverflow.com/q/43062278/643383) – Caleb

+0

@ReinierMelianwishそれは簡単でした。 indexpath.rowでは1つのセルだけを配置しますが、他のセルは配置しません – SoundShock

答えて

0

広告フォルダの両方を表示したい場合は、行の数を増やす必要があります

override func numberOfItems(inSection section: Int) -> Int { 
    let count = //the count you were using before 
    return count + floor(count/4) //add an ad every 4 
} 

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if indexPath.row % 4 == 0 && indexPath.row > 0 { //assuming you don't want an ad as the first item 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdUnitsCollectionViewCell", for: indexPath) as? AdUnitsCollectionViewCell 
     //cell?.addSubview(AdUnitController().getBannerView(2)) 
     //cell?.view = AdUnitController().getBannerView(2) 
     cell?.view.adUnitID = "/6499/example/banner" 
     cell?.view.rootViewController = self 
     cell?.view.load(DFPRequest()) 
     return cell! 
    } else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell 
     cell!.configure(folders![indexPath.row]) 
     return cell! 
    } 
} 

EDIT

より総合的なアプローチがあなたのfoldersを修正するだろう配列(?)は、AdUnitsCollectionViewCellFolderViewCellの両方のデータをサポートします。
folders

struct CellData { 
    static enum CellDataType { 
     case folder 
     case ad 
    } 
    let type: CellDataType 
    let data: //some type - you haven't shown whats required 
} 

として構造体の配列である場合たとえば、folders配列にすべての4広告を注入する方法があるように必要があるでしょうし、あなたがしたい場合は、あなたが決めることfolders[indexPath.row].typeに切り替えることができますが広告セルまたはフォルダセルを表示します。 numberOfItemsInSectionメソッドを変更する必要はありません。

+2

これは、フォルダのコレクションのサイズ外のインデックスになる可能性があります。行数を追加するだけでは不十分です。実際のセルを構成するときは、負のオフセットを考慮する必要があります。それ以外の場合は、フォルダコレクション内の行をスキップしているだけで、最終的にはフォルダコレクションの境界外に出ます。 – bhmahler

+0

@bhmahlerクラス全体を書き込むことは期待できません。彼は 'フォルダ'データがどのように見えるかを共有していません。私はこれが彼が正しい方向に向かうのに役立つと思う。 – dmorrow

1

まず、コレクションビュー(total_folder + total_add)に追加する場合は、項目/行の総数を増やす必要がありますが、として@bhmahlerは言っています。十分に、あなたは、あなたが何ができるか

を負のオフセットを考慮する必要がありますが、ブロック場合には、このブロック増加の内側に数

を行った場合、それは、 内部で行ってきましたどのように多くの時間のカウントを保持することができますです
if indexPath.row == 4 || indexPath.row == 9 || indexPath.row == 14 { 
    count+=1 //which is initially set to zero 
} 

あなたのelseブロック私は迅速知らないとして、あなたは単にできマイナスコレクションビューのデリゲートメソッドのインデックスパスからのカウント、この

else { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell 
     cell!.configure(folders![indexPath.row-count]) 
     return cell! 
    } 

よう、カウント= 0外に設定してください

不完全なコードのために申し訳ありません

.Wopeが役に立ちます。

関連する問題