2017-04-04 115 views
0

所以我想在我的UIcollectionView中添加廣告。每隔4個文件夾後,我想顯示一個廣告(總共3次,因此第4,9和14行)。UICollectionView不同的單元格每個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)。有沒有一種方法可以將我的廣告添加到collectionview和我的文件夾中?

謝謝!

+0

您需要在您的tableViewDataSource –

+0

又見你的'numberOfRowsInSection'方法添加+3 [如何集合視圖中顯示廣告(http://stackoverflow.com/q/43062278/643383) – Caleb

+0

@ReinierMelianwish就這麼簡單。給我一個索引是越界(必須小於187)',因爲在indexpath.row我只放置一個單元格,但不是其他 – 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! 
    } 
} 

編輯

更全面的方法是修改您的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 
} 

有需要是注入的廣告,每4到folders陣列的方法,然後你可以打開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 { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell 
     cell!.configure(folders![indexPath.row-count]) 
     return cell! 
    } 

務必將數= 0外的集合視圖的委託方法

對不起,不完整的代碼,我不知道SWIFT 。希望它有助於

相關問題