How to show ads as a collection

I try to add some banner ads randomly in my CollectionView.

Each collectionView cell will be a main image (a black square here to simplify the task) dynamically populated from the array (let it be a really long array and call it "longDataArray"), which I would get from the Internet.

I managed to add some banner ads to my CollectionView, but the problem is that it upsets my longDataArray. For example, for testing, when I add an ad banner in indexPath 6, the ad banner displays correctly in indexPath 6 and I can control the width change for the cell , but the image corresponding in indexPath 6 for my longDataArray will obviously never appear.

I could also do this by splitting my longDataArray into two parts and then playing with sections: section 0 = firstPartOfArray, section 1 = banner ad and section 2 = secondPartOfArray. But this requires a lot of effort, creating various arrays and sections, just adding only one banner ad, and this is clearly not what I'm looking for.

So my question is: how to add a banner ad to my CollectionView (only one section), but keeping the indexPath logic?

I talked a lot about this, and was surprised that I could not come up with any solution to this problem.

Do you have any ideas?

Thanks!

screenshot

+8
ios uicollectionview ads
source share
3 answers

Hi, I created a View collection inside the table cell with the same requirement, you can check my code.

// // 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]) } } } 
+3
source share

For UICollectionView you need to execute two custom UICollectionView

  • Cell1 for Imageview .
  • Cell2 is for an advertising banner.

In cellForItem

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

Deploy UICollection Open delegate stream view and use this as

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

To show ads in your application, you are AdMob

+2
source share

So my question is: how do I add a banner ad to my collector element (only one section), but keeping the indexPath logic?

You just need to set up the index path for the account for ads. For example, let's say that you want every 15th cell to contain an ad. Let here we use arithmetic based on 1 to make mathematics intuitive. Cells 1-14 will simply receive their regular content, cell 15 will have an ad, cells 16-29 will receive content for items 15-28, cell 30 will receive another ad, etc. Thus, your method -collectionView:cellForItemAtIndexPath: needs to find out if the pointer path refers to the ad cell (in this case, the element number based on 1 is evenly divided by 15) or the content cell (every other cell). In the latter case, he will also need to adjust the position number to obtain the desired content.

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

You will also have to perform the corresponding calculations in other cell related methods, for example -collectionView:numberOfItemsInSection: For this reason, it would be nice to write some useful methods that would make adjustments.

+2
source share

All Articles