如何在集合视图中显示广告

时间:2017-03-28 06:37:49

标签: ios uicollectionview ads indexpath

我试图在我的collectionView中随机添加一些横幅广告。

每个collectionView单元格都是一个基本图像(这里的黑色方块更容易)从一个数组中动态填充(让我们说它是一个非常长的数组并调用它" longDataArray&#34 ;)我会从网上获得。

我可以设法在我的collectionView中添加一些横幅广告,但问题是它违反了我的longDataArray的顺序。 例如,仅在我在indexPath 6添加广告横幅时进行测试,然后广告横幅正确地显示在indexPath 6上,并且我能够管理单元格的宽度更改,但是我的longDataArray在indexPath 6上对应的图像显然永远不会出现。

我也可以将我的longDataArray分成两部分,然后玩部分:section 0 = firstPartOfArray,section 1 = ad banner,section 2 = secondPartOfArray。但这需要花费大量精力创建不同的数组和部分,只需添加一个广告横幅,而且它显然不是我想要的。

所以我的问题是,您如何在collectionView中添加横幅广告(仅限一个部分),但保留indexPath逻辑?

我搜索了很多关于此事的信息,并对我无法解决此问题感到惊讶。

你们有什么想法吗?

谢谢!

screenshot

4 个答案:

答案 0 :(得分:3)

您好我在表格单元格中创建了一个具有相同要求的collectionView,您可以检查我的代码。

//
//  ViewController.swift
//  DemoApp
//
//  Created by Mahesh Kumar on 09/01/18.
//  Copyright © 2018 Mahesh Kumar. All rights reserved.
import UIKit

class TableCell : UITableViewCell{
    @IBOutlet weak var collVw: UICollectionView!
    @IBOutlet weak var categoryName: UILabel!
}

class ViewController: UIViewController, UICollectionViewDelegate,UICollectionViewDataSource , UICollectionViewDelegateFlowLayout , UITableViewDelegate,UITableViewDataSource {

    var categories_array = ["Home","Helth","New","Home1","Home2","Home3","Home4","Home5","Home6","Home7","Home8","Home9","Home11","Home12","Home13","Home14","Home15"]

    //Mark
    var sectionArray = NSMutableArray()


    @IBOutlet weak var tableVw: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()

        //Mark
        var sectionCount = 0
        var mainIndex = 0

        let section = categories_array.count % 4
        if(section == 0){
            sectionCount =  categories_array.count/4
        }
        else{
             sectionCount =  categories_array.count/4 + 1
        }
        //Mark
        for _ in 0...sectionCount {
            let rowsData = NSMutableArray()
            var j = 0
            while  j<4{
                if(mainIndex == categories_array.count){
                    break
                }
                rowsData.add(categories_array[mainIndex])
                j = j + 1
                mainIndex = mainIndex + 1

            }
          sectionArray.add(rowsData)


        }

        tableVw.reloadData()

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableVw.dequeueReusableCell(withIdentifier: "cell1", for: indexPath) as? TableCell

        if(indexPath.row == 1){
            cell?.categoryName.text = "Top Redeemed"
        }
        else if(indexPath.row == 2){
            cell?.categoryName.text = "Categories"
        }

        cell?.collVw.tag  = indexPath.row
        cell?.collVw.delegate = self
        cell?.collVw.dataSource = self
        cell?.collVw.reloadData()

        return cell!
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return 400
    }


    func numberOfSections(in collectionView: UICollectionView) -> Int {
        if(collectionView.tag == 0){
              return 1
        }
        else if(collectionView.tag == 1){
            if(categories_array.count > 4){
            if(categories_array.count % 4 == 0){
                return categories_array.count/4
            }
            else{
                return (categories_array.count/4) + 1
            }
            }
            else{
                return 1
            }

        }
        else{
            return 10
        }


    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = UIColor.green
        if let lbl = cell.viewWithTag(1) as? UILabel{
            lbl.text = "\(indexPath.row)"
        }
        return cell

    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        if(collectionView.tag == 0){
             return CGSize.init(width:  collectionView.frame.width - 10, height: collectionView.frame.height)
        }
        else if(collectionView.tag == 1){
            return CGSize.init(width:  (collectionView.frame.width)/2 - 5.5, height: collectionView.frame.height/2 - 0.5)

        }
       else {
            return CGSize.init(width:  collectionView.frame.width/3 - 4, height: collectionView.frame.height/2 - 0.5)
        }

    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if(collectionView.tag == 0){
            return 10
        }
        else{
            return 1
        }
    }

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
   {
       return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
   }




    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if(collectionView.tag == 0){
           return 10
        }
         else if(collectionView.tag == 1){
            return ((sectionArray[section] as? NSMutableArray)?.count)!
        }
         else{
             return 6
        }

    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("coll vw",indexPath.row)
        print(indexPath.section)

        //Mark
        if(collectionView.tag == 1){
            print(collectionView.tag)
            let array = sectionArray[indexPath.section] as? NSMutableArray
            print(array![indexPath.row])
        }

    }


}

答案 1 :(得分:2)

对于UICollectionView,您必须有两个自定义UICollectionView

  1. Cell1代表Imageview
  2. Cell2用于横幅广告。
  3. cellForItem

    if (indexPath.item == 6){
        // dequeue your cell2 here
        return cell2
    }
    else{
        // dequeue your cell1 here
        return cell1
    }
    

    实施UICollection​View​Delegate​Flow​Layout并像这样使用

    func collectionView(_ collectionView: UICollectionView, 
                          layout collectionViewLayout: UICollectionViewLayout, 
                   sizeForItemAt indexPath: IndexPath) -> CGSize{
    
      if (indexPath.item == 6){
          return CGSizeMake(60,60)
      }
      else{
          return CGSizeMake([[UIScreen mainScreen] bounds].size.width, 60.0)
      }
    
    }
    

    要在您的应用中展示广告,请AdMob

答案 2 :(得分:2)

  

所以我的问题是,你如何在collectionView中添加横幅广告(只有一个部分),但保留indexPath逻辑?

您只需调整索引路径即可考虑广告。例如,假设您希望每第15个单元格包含一个广告。让我们在这里使用基于1的算术来使数学直观。单元格1-14将获得其常规内容,单元格15将具有广告,单元格16-29将获得项目15-28的内容,单元格30将获得另一广告,等等。因此,您的-collectionView:cellForItemAtIndexPath:方法需要确定索引路径是指广告单元格(在这种情况下,基于1的项目编号可被15整除)或内容单元格(每隔一个单元格)。在后一种情况下,还需要调整项目编号以获得正确的内容。

NSInteger item = indexPath.item + 1;   // switch to 1-based numbering
if (item % 15 == 0) {
    // we have an ad cell, so return a cell configured with an ad
}
else {
    item = item - (item / 15); // subtract the ad cells
    item -= 1;                 // switch back to 0-based indexes
    // return a cell configured with the data at index `item`
}

您还必须在处理细胞的其他方法中进行相应的计算,例如-collectionView:numberOfItemsInSection:。出于这个原因,编写一些可以进行调整的实用方法可能是个好主意。

答案 3 :(得分:2)

我们成功地为此问题与Ersin合作开发了一个示例。

您可以在此处查看。

https://github.com/Cemoo/WaterFlowLayout