Search code examples
swiftuitableviewswitch-statementincrementindexpath

how to check indexpath.row == 4 * n by n += 1


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 

Solution

  • 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