如何通过 n += 1 检查 indexpath.row == 4 * n

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

问题陈述:我想dequeueReusableCell"PromotionCellIdentifier"到处都是单元格indexPath.row == 4 * n但它只有运行 dequeueReusableCell "PromotionCellIdentifier" 一次。我需要一个解决方案。

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

我试过的:

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
        }

    }

所需输出: I want to implement in this way

我的输出: This is my result 1 & This is my result 2

我需要:

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 

每次调用 cellForRowAt 时,n 的值都会重置为 1。您需要在函数范围之外声明 n 才能正常工作。

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

更好的方法:更好的方法是使用 indexPath.row % 4 == 0 或最新的 swift 语法 indexPath.row.isMultiple(of: 4),后者更具可读性。

您的方法将无法正常工作,因为无法保证单元格的加载顺序。如果用户向上滚动,单元格 9 可能会出现在单元格 4 之前。相反,您应该使用 mod 运算符 %.

检查 5 的倍数

将支票更改为:

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

此外,这将用促销单元格替换 第 4、9、14 等单元格。我怀疑您真的只想插入促销单元格而不丢失任何信息单元格。在这种情况下,您需要移动索引路径以说明促销单元格:

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

这会给你一个 table 这样的:

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

最简单的方法是使用 indexPath.row.isMultiple(of: 4) 而不是 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
}

}