2

Problem Statement : I want to dequeueReusableCell "PromotionCellIdentifier" cells everywhere indexPath.row == 4 * n but it only run dequeueReusableCell "PromotionCellIdentifier" one time . I need a solution.

It have only dequeueReusableCell "PromotionCellIdentifier" one time because n == 1 not increment += 1

What I tried :

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let homeSection = Section(rawValue: indexPath.section)!

        var n = 1

        switch homeSection {
        case .Promotion:
            let cell = tableView.dequeueReusableCell(withIdentifier: "",
                                                     for: indexPath)
            return cell

        case .Information:

            if indexPath.row == 4 * n{

                let cell = tableView.dequeueReusableCell(withIdentifier: "PromotionCellIdentifier",
                                                         for: indexPath)
              n += 1
                return cell
            }

            let cell = tableView.dequeueReusableCell(withIdentifier: "MenuItemCellIdentifier",
                                                     for: indexPath) as! MenuTableViewCell


            let index = indexPath.row != 0 ? indexPath.row - 1 : 0
            let menu = menuItems[index]


            let number = menu.like
            let stringNumber = numberdecimal(number: number)

            cell.configure(imageName: menu.imageName, title: menu.name, detail: menu.details, like: stringNumber,nameLike: menu.likeImage)
            return cell
        }

    }

Required Output : I want to implement in this way

My Output : This is my result 1 & This is my result 2

I need:

adjust row 
adjust row 
adjust row 
adjust row 
promotion row
adjust row 
adjust row 
adjust row 
adjust row 
promotion row
adjust row 
adjust row 
adjust row 
adjust row 
vacawama
  • 150,663
  • 30
  • 266
  • 294

3 Answers3

1

Your approach won't work correctly because there is no guarantee the order the cells will be loaded. Cell 9 might come before cell 4 if the user is scrolling upwards. Instead, you should just check for multiples of 5 using the mod operator %.

Change your check to:

if indexPath.row % 5 == 4 {
    // special handling here for rows 4, 9, 14, ...

Also, this will replace the 4th, 9th, 14th, etc. cell with a promotional cell. I suspect you really just want to insert the promotional cell without losing any of the information cells. In that case, you'll want to shift the index paths to account for the promotional cells:

if indexPath.row % 5 == 4 {
    // special handling here for rows 4, 9, 14, ...

    return cell
}

// compute new adjustedRow to account for the insertion of the
// promotional cells
let adjustedRow = indexPath.row - indexPath.row / 5

which would give you a table like this:

0:  adjusted row 0
1:  adjusted row 1
2:  adjusted row 2
3:  adjusted row 3
4:  promotional cell
5:  adjusted row 4
6:  adjusted row 5
7:  adjusted row 6
8:  adjusted row 7
9:  promotional cell
10: adjusted row 8
11: adjusted row 9
12: adjusted row 10
13: adjusted row 11
14: promotional cell
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • i want to 0: adjusted row 0 1: adjusted row 1 2: adjusted row 2 3: adjusted row 3 4: promotional cell 5: adjusted row 4 6: adjusted row 5 7: adjusted row 6 8: adjusted row 7 9: promotional cell 10: adjusted row 8 11: adjusted row 9 12: adjusted row 3 13: adjusted row 10 14 : promotional cell – Chaiyapod Tuntimanigun Jun 07 '20 at 13:03
0

Each time cellForRowAt is called the value of n resets back to 1. You need to declare n outside the function scope for this to work.

var n = 1
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    //...
}

Better Approach: A better approach would be to use indexPath.row % 4 == 0 or the latest swift syntax indexPath.row.isMultiple(of: 4) which is more readable.

Frankenstein
  • 15,732
  • 4
  • 22
  • 47
0

The simplest way is using indexPath.row.isMultiple(of: 4) instead indexPath.row == 4 * n

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

let homeSection = Section(rawValue: indexPath.section)!

switch homeSection {
case .Promotion:
    let cell = tableView.dequeueReusableCell(withIdentifier: "",
                                             for: indexPath)
    return cell

case .Information:

    if indexPath.row.isMultiple(of: 4) {

        let cell = tableView.dequeueReusableCell(withIdentifier: "PromotionCellIdentifier",
                                                 for: indexPath)

        return cell
    }

    let cell = tableView.dequeueReusableCell(withIdentifier: "MenuItemCellIdentifier",
                                             for: indexPath) as! MenuTableViewCell


    let index = indexPath.row != 0 ? indexPath.row - 1 : 0
    let menu = menuItems[index]


    let number = menu.like
    let stringNumber = numberdecimal(number: number)

    cell.configure(imageName: menu.imageName, title: menu.name, detail: menu.details, like: stringNumber,nameLike: menu.likeImage)
    return cell
}

}

Dmytro
  • 131
  • 4