title: How to add a custom view after every 2 rows in a collectionView swift
body: <p>I am implementing general collectionView in a viewController to populate data and the collection view has 2 columns and the number of rows depend on the data, and now my collectionView looks like this.</p>
  
  <p>Normal collectionView:</p>
  
  <p><a href="https://i.stack.imgur.com/ooBUX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ooBUX.png" alt="enter image description here"></a></p>
  
  <p>This is what I have implemented in my app as you can see it is a normal collection view with n rows and 2 columns. But, our requirement is</p>
  
  <p>Business requirement Image:</p>
  
  <p><a href="https://i.stack.imgur.com/paD2B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/paD2B.png" alt=""></a></p>
  
  <p>There is the custom view which is added after every 2 rows and it is static with a two labels and a button...</p>
  
  <p>I don't know if it is possible and how to achieve this... And after searching for some time I learned that we can do this by using DecoratorViews and I don't know what are those and how to use them.. If anyone have any idea on how to achieve this kind of layout, please guide me..</p>
  
  <p>variables:</p>
  
  <pre><code>let columnsPerRow = 2
  let addAfterRows = 5
  var cellToShowWithAdds = 0
  </code></pre>
  
  <p>Function:</p>
  
  <pre><code>func getCategoryProducts() {
  
      var id = Int()
      var categoryProductsAPI = ""
      if self.brandId != nil {
          id = self.brandId!
          if self.selectedSubCategoryId != nil {
              categoryProductsAPI = "\(API.CATEGORY_BRAND_FILTER)\(self.selectedSubCategoryId!)\(API.BRAND_ID )\(id)"
          } else {
              categoryProductsAPI = "\(API.CATEGORY_BRAND_FILTER)\(self.categoryId!)\(API.BRAND_ID )\(id)"
          }
      } else {
          if self.selectedSubCategoryId != nil {
              id = self.selectedSubCategoryId!
          } else {
              id  = self.categoryId!
          }
          categoryProductsAPI = "\(API.CATEGORY_PRODUCTS)\(id)"
      }
      print(categoryProductsAPI)
      self.cellToShowWithAdds = 0
      self.categoryProductsData = []
      self.loadingView.isHidden = false
      self.loadingActivityIndicator.animate()
      ServiceManager.callGetAPI(url: categoryProductsAPI, view: self, closure: { response in
          self.loadingView.isHidden = true
          self.loadingActivityIndicator.stopAnimating()
          guard let categoryData = response?.result.value  else {return}
          if let categories = categoryData as? [[String : Any]] {
              for product in categories {
                  let productName = product["product_name"] as! String
                  let productId = product["product_id"] as! String
                  let productBrand = product["product_brand"] as! String
                  guard let productOffPercent = product["product_sale_of"] else { return }
                  let productImage = product["product_image"] as! String
                  let productPrice = product["product_price"] as! String
                  let productSepcialPrice = product["product_special_price"] as! String
                  var newProductPrice = String()
                  if productSepcialPrice == "Rs.0.00" {
                      newProductPrice = productPrice
                  } else {
                      newProductPrice = productSepcialPrice
                  }
  
                  self.categoryProductsData.append(ProductDetails(productID: productId, productName: productName, productPrice: productPrice, productSpecialPrice: newProductPrice, productOff: productOffPercent, productBrand: productBrand, productImageURL: productImage))
              }
              let quot = (self.categoryProductsData.count/(self.columnsPerRow * self.addAfterRows))
              self.cellToShowWithAdds = self.categoryProductsData.count + quot + 1
              DispatchQueue.main.async {
                  self.categoryProductsCollection.reloadData()
              }
          }
      }, errorAction: {
          self.loadingView.isHidden = true
          self.loadingActivityIndicator.stopAnimating()
      }, okAction: {
          self.view.setNeedsLayout()
          self.viewWillAppear(true)
      })
  }
  </code></pre>
  
  <p>DataSource methods:</p>
  
  <pre><code>func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
  
      return cellToShowWithAdds        
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
     if indexPath.row % 5 != 0 {
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath) as! ProductDisplayCell
              let productId = Int(categoryProductsData[indexPath.item].productID)
              cell.tag = productId!
              if categoryProductsData[indexPath.item].productImageURL != "" {
                  if let productImage = URL(string: categoryProductsData[indexPath.item].productImageURL) {
                      cell.productImage.getImageWith(imageUrl: productImage)
                  }
              } else {
                  cell.productImage.image = nil
              }
              cell.productNameLabel.text = categoryProductsData[indexPath.item].productName
              cell.sellerNameLabel.text = categoryProductsData[indexPath.item].productBrand
              cell.offerPercentLabel.text = "\(categoryProductsData[indexPath.item].productOff)% Off"
  
              if "\(categoryProductsData[indexPath.item].productOff)" == "" || "\(categoryProductsData[indexPath.item].productOff)" == "100" || "\(categoryProductsData[indexPath.item].productOff)" == "0" {
                  cell.offerPercentLabel.isHidden = true
              } else {
                  cell.offerPercentLabel.isHidden = false
              }
  
              if categoryProductsData[indexPath.item].productSpecialPrice != "Rs.0.00" {
                  if categoryProductsData[indexPath.item].productPrice == categoryProductsData[indexPath.item].productSpecialPrice  {
                      cell.originalPriceLable.isHidden = true
                      cell.offerPriceLabel.isHidden = false
                  } else {
                      cell.originalPriceLable.isHidden = false
                      cell.offerPriceLabel.isHidden = false
                  }
              } else if categoryProductsData[indexPath.item].productSpecialPrice == "Rs.0.00" {
                  cell.originalPriceLable.isHidden = true
                  cell.offerPriceLabel.isHidden = true
              } else {
                  cell.originalPriceLable.isHidden = false
                  cell.offerPriceLabel.isHidden = false
              }
              cell.originalPriceLable.attributedText = categoryProductsData[indexPath.item].productPrice.strikeThrough()
              cell.offerPriceLabel.text = categoryProductsData[indexPath.item].productSpecialPrice
              return cell
          } else {
              let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "formCollectionCell", for: indexPath) as! PostRequirementCellCollectionViewCell
              return cell
          }       
  }
  </code></pre>