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

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

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

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

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

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

我搜索了很多关于这一点,并且惊讶于我无法为这个问题找到任何解决方案.

你们有什么想法吗?

谢谢!

解决方法

您好我已经在表格单元格内创建了一个具有相同要求的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,minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if(collectionView.tag == 0){
            return 10
        }
        else{
            return 1
        }
    }

   func collectionView(_ collectionView: UICollectionView,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])
        }

    }


}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...