indexpath.row reset after 3 inside tableView heightForRowAt

545 views Asked by At

Hi like the title says the index.row never goes to 4 instead it starts over to [2,0]. My tableView has 5 rows. I don't know when it stopped working but I know for sure that it worked before I added the timeIndex row.

It's the "Enter Address Here" field which I'm having a problem displaying

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

}

tableview

Console output

Console output

3

There are 3 answers

0
CFRJ On BEST ANSWER

I found the problem I forgot to update the number of rows inside the numberOfRowsInsideSection function

 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
}
0
sarosh mirza On

You can check what you have in numberOfRowsInSection

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

}

Should be wrong number of rows being set in the numberOfRowsInSection

1
Irshad Ahmad On

Each section will have different number of rows. that's why your indexPath never goes to 4. [2,0] mean row number 0 in section 2. you can check row according to section like this:-

    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
         }
    }

}