所以我想在我的UIcollectionView中添加广告。在每个第四个文件夹之后,我想要显示一个广告(总共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!
}
}
这确实在每个第四个文件夹后显示广告3次,但现在我的文件夹不再显示(4,9和14)。有没有办法在收藏夹和我的文件夹中添加广告?
谢谢!
答案 0 :(得分:1)
首先,如果你想在集合视图中添加它(必须是total_folder + total_add),你必须增加项目/行的总数,但是作为 @bhmahler说:只需添加行数是还不够,你需要考虑负偏移
你可以做的是你可以计算它在 if block 里面的时间,如果它进入这个区块增加了计数
else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FolderViewCellIdentifier, for: indexPath) as? FolderViewCell
cell!.configure(folders![indexPath.row-count])
return cell!
}
现在在你的else块中你可以简单地减去索引路径中的计数,就像这样
{
"name": "SemanticModel",
"compatibilityLevel": 1200,
"model": {
"culture": "en-US",
"dataSources": [
{
"name": "BlahDW",
"connectionString": "Provider=SQLOLEDB;Data Source=sql.blah.com;Persist Security Info=false;Integrated Security=SSPI;Initial Catalog=Blah",
"impersonationMode": "impersonateAccount",
"account": "blah\\blah",
"annotations": [
{
"name": "ConnectionEditUISource",
"value": "SqlServer"
}
]
}
],
"tables": [
{
"name": "Employees",
"isHidden": true,
"columns": [
{
"name": "Employee Key",
"dataType": "int64",
"isHidden": true,
"isUnique": true,
"isNullable": false,
"sourceColumn": "Employee Key"
},
{
"name": "Employee Code",
"dataType": "string",
"isHidden": true,
"sourceColumn": "Employee Code"
},
{
"name": "Employee Name",
"dataType": "string",
"isHidden": true,
"sourceColumn": "Employee Name"
},
{
"name": "Home Village Code",
"dataType": "string",
"isHidden": true,
"sourceColumn": "Home Village Code"
}
],
....
确保在集合视图委托方法
之外设置 count = 0对不完整的代码感到抱歉,因为我不知道swift。希望它有帮助
答案 1 :(得分:0)
如果您要同时展示广告 AND 文件夹,则需要增加行数
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
数组(?)以支持AdUnitsCollectionViewCell
和FolderViewCell
数据。
例如,如果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
方法。