swiftuitableviewheightforrowatindexpathindexpath

indexpath.row reset after 3 inside tableView heightForRowAt


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


Solution

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