Home > other >  How to show AdCell for every three cells of table view in swift
How to show AdCell for every three cells of table view in swift

Time:02-04

I have to show Adcell for every three cells after. for that I have taken two prototype cells and designed adcell in one and normal cell in other

code: with my code only after first three rows AdCell showing and lost 3rd row from JobsCell from 4th row data showing

how to show Adcell for every cells and without losing JobsCell data. please guide

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return categoryData?.result?.categoryData?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var returnCell: UITableViewCell!

if indexPath.row == 3  {
    returnCell = tableView.dequeueReusableCell(withIdentifier: "AdCell", for: indexPath) as! AdCell
}

else {
    returnCell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! JobsCell
    returnCell.selectionStyle = .none
}
return returnCell
}

CodePudding user response:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
   var count = categoryData?.result?.categoryData?.count ?? 0
   return count   count / 3
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var returnCell: UITableViewCell!

if indexPath.row % 3 == 0 && indexPath.row != 0  {
    returnCell = tableView.dequeueReusableCell(withIdentifier: "AdCell", for: indexPath) as! AdCell
}

else {
    returnCell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! JobsCell
    returnCell.selectionStyle = .none
}
return returnCell
}

CodePudding user response:

The first problem: in your example func(numberOfItemsInSection) returns less rows than need. Therefore, to begin with, adding the missing cells.

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

   let n = categoryData?.result?.categoryData?.count ?? 0

   //for example here 10 jobs
   //3j 1a 3j 1a 3j 1a 1j(last) Here 3ads
   //10/3 = 3(and 1)
   //so will 11/3=3(2), then 12/3=4(0)
   //So generic formula: num = j   j/3

   let num = n   n/3   //n is Int so division rounds the number down

   return num
}

Second problem: when adding an extra cell in indexPath, it gonna lost fourth job if using data[indexPath.row]. So it possible to create variable like "jumpDownNumber". It will save the step to the skipped indexPath.row number.

var jumpDownNumber = 0 //and reload it before tableView.reloadData()

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   var returnCell: UITableViewCell!
   
   //after 3 jobCell is every fourth AD cell
   //but due to the fact that the count starts from 0, the first AD will be after 4 cells, and then, as usual, after 3

   if indexPath.row % 4 == 0 && indexPath.row != 0 {
      returnCell = tableView.dequeueReusableCell(withIdentifier: "AdCell", for: indexPath) as! AdCell
      jumpDownNumber  = 1
   } else {
    returnCell = tableView.dequeueReusableCell(withIdentifier: "JobsCell", for: indexPath) as! JobsCell

    //here you can use data[indexPath.row-jumpDownNumber]
    returnCell.selectionStyle = .none
   }

   return returnCell
}
  • Related