【问题标题】:how to check indexpath.row == 4 * n by n += 1如何检查 indexpath.row == 4 * n by n += 1
【发布时间】:2020-09-26 09:34:38
【问题描述】:

问题说明:我想 dequeueReusableCell "PromotionCellIdentifier" 任何地方的单元格 indexPath.row == 4 * n 但它只运行 dequeueReusableCell "PromotionCellIdentifier" 一次。我需要一个解决方案。

它只有一次 dequeueReusableCell "PromotionCellIdentifier" 因为 n == 1 不递增 += 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 

【问题讨论】:

    标签: swift uitableview switch-statement increment indexpath


    【解决方案1】:

    您的方法无法正常工作,因为无法保证单元格的加载顺序。如果用户向上滚动,单元格 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
    

    这会给你一个像这样的表格:

    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
    

    【讨论】:

    • 我要 0:调整第 0 行 1:调整第 1 行 2:调整第 2 行 3:调整第 3 行 4:促销单元格 5:调整第 4 行 6:调整第 5 行 7:调整第 6 行8:调整第 7 行 9:促销单元格 10:调整第 8 行 11:调整第 9 行 12:调整第 3 行 13:调整第 10 行 14:促销单元格
    【解决方案2】:

    每次调用cellForRowAtn 的值都会重置回1。您需要在函数范围之外声明 n 才能使其工作。

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

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

    【讨论】:

      【解决方案3】:

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

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-19
        • 1970-01-01
        • 2011-02-14
        • 2011-08-03
        • 1970-01-01
        • 2012-07-30
        • 1970-01-01
        相关资源
        最近更新 更多