【问题标题】:indexpath.row reset after 3 inside tableView heightForRowAtindexpath.row 在tableView heightForRowAt 3 后重置
【发布时间】:2017-07-26 09:04:17
【问题描述】:

嗨,就像标题所说的那样,index.row 永远不会达到 4,而是从 [2,0] 开始。我的 tableView 有 5 行。我不知道它什么时候停止工作,但我确信在我添加 timeIndex 行之前它已经工作了。

这是我在显示时遇到问题的“在此处输入地址”字段

let timeIndex = 2
let DateLocation = 1
let locationIndex = 4
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {



    if userPickedDate && indexPath.row == timeIndex {
        return 50
    }
    if userPickedDate && indexPath.row == DateLocation {

        return 0

    }
    print("The indexPath: \(indexPath)")
    if remindMeOnLocationSwitch.isOn && indexPath.row == locationIndex {
        return 100

    }
    if remindMeOnDay.isOn && indexPath.row == DateLocation{
        return 300
    } else if indexPath.row == DateLocation || indexPath.row == timeIndex || indexPath.row == locationIndex  {
        return 0
    }
    return 50

}

控制台输出

【问题讨论】:

  • 表格视图中有多少个部分?
  • 3 一个在第一个文本框的位置,然后一个在中间的提醒meomday 开始的地方,最后一个是[今天一周的待办事项]
  • 我发布了一个答案,你可以看看希望你能理解为什么你的 indexPath 永远不会变成 4

标签: swift uitableview heightforrowatindexpath indexpath


【解决方案1】:

您可以检查 numberOfRowsInSection 中的内容

    func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int
{
    if(section == 0){
    return 1
    }
    else if(section == 1){
        return 2
    }else{
        return 3
    }

}

在 numberOfRowsInSection 中设置的行数应该是错误的

【讨论】:

    【解决方案2】:

    每个部分将有不同的行数。 这就是为什么您的 indexPath 永远不会达到 4 的原因。 [2,0] 表示第 2 节中的第 0 行。 您可以根据这样的部分检查行:-

        override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0{
    
        }else if indexPath.section == 1{
    
        }else if indexPath.section == 2{
             if indexPath.row == 0{
                // here is your fifth row return height for fifth row
             }
        }
    
    }
    

    【讨论】:

    • 嘿,我不确定您是否错过了了解我的信息,但我无法显示的是“在此处输入地址”行,而不是 segmentControll
    【解决方案3】:

    发现了忘记更新numberOfRowsInsideSection函数内的行数的问题

     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        // Change from [1,4,1] to [1,5,1]
        let numberOfRowsInSection: [Int] = [1,5,1]
        var rows: Int = 0
    
        if section < numberOfRowsInSection.count {
            rows = numberOfRowsInSection[section]
        }
    
        return rows
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      相关资源
      最近更新 更多