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

问题描述 投票:1回答:3
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
        }

    }

我想让dequeueReusableCell“ PromotionCellIdentifier”单元遍及indexPath.row == 4 * n,但它只运行dequeueReusableCell“ PromotionCellIdentifier”一次。我需要解决方案。

i want to be that

that is my result 1that is my result 2

一次只有dequeueReusableCell“ PromotionCellIdentifier”,因为n == 1而不是增量+ = 1

swift uitableview switch-statement increment indexpath
3个回答
0
投票

每次调用cellForRowAt时,n的值将重置为1。您需要在函数范围之外声明n才能使它起作用。

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

更好的方法:更好的方法是使用indexPath.row % 4 == 0


0
投票

最简单的方法是使用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
}

}


0
投票

您的方法无法正常工作,因为不能保证将加载单元格的顺序。如果用户向上滚动,则单元格8可能在单元格4之前。相反,您应该只使用mod运算符%检查4的倍数。由于您要跳过第0行,因此为此添加一个附加检查。

将支票更改为:

if indexPath.row > 0 && indexPath.row % 4 == 0 {
    // special handling here for rows 4, 8, 12, ...

此外,这将替换具有促销单元格的第4、8、12等单元格。我怀疑您真的只想插入促销单元而不丢失任何信息单元。在这种情况下,您需要转移索引路径以考虑促销单元:

if indexPath.row > 0 && indexPath.row % 4 == 0 {
    // special handling here for rows 4, 8, 12, ...

    return cell
}

// compute new adjustedRow to account for the insertion of the
// promotional cells
let adjustedRow = indexPath.row - indexPath.row / 5
© www.soinside.com 2019 - 2024. All rights reserved.