10

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

每个collectionView 单元格都是一个基本图像(这里是黑色方块以使事情更容易)从我从网上获得的数组(假设它是一个非常长的数组并称之为“longDataArray”)动态填充。

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

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

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

我在谷歌上搜索了很多,很惊讶我无法为这个问题想出任何解决方案。

你们有什么想法吗?

谢谢!

截屏

4

4 回答 4

3

对于UICollectionView你必须两个自定义UICollectionView

  1. Cell1 用于Imageview.
  2. Cell2 用于横幅广告。

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

于 2017-03-28T06:43:53.283 回答
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])
        }

    }


}
于 2018-01-19T04:37:06.567 回答
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:. 出于这个原因,编写一些可以进行调整的实用方法可能是一个好主意。

于 2017-03-28T07:08:14.060 回答
2

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

你可以在这里查看。

https://github.com/Cemoo/WaterFlowLayout

于 2018-07-23T22:07:31.340 回答