簡體   English   中英

UICollection每x個單元格查看一個不同的單元格

[英]UICollectionView different cell every x cells

所以我想在UIcollectionView內添加廣告。 在第4個文件夾之后,我想展示一個廣告(共3次,因此第4,9和14行)。

我嘗試了以下方法:

  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!
        }
    }

確實確實在每個第4個文件夾之后3次顯示廣告,但是現在我的indexPath.row文件夾不再顯示(4、9和14)。 有什么方法可以將廣告添加到collectionview和文件夾中嗎?

謝謝!

首先,如果要在集合視圖中添加項目/行,則必須增加項目/行的總數(將為total_folder + total_add),但正如@bhmahler所說:僅添加行數是不夠的,那么您需要抵消負偏移

您可以做的是,您可以記錄它進入if塊的時間 ,如果它進入此塊,可以增加計數

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!
    }

確保在集合視圖委托方法之外設置count = 0

對不完整的代碼感到抱歉,因為我不知道swift.Hope它有幫助

如果要同時顯示廣告文件夾,則需要增加行數

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方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM