Tableview crash in height for row At indexPath

322 views Asked by At

enter image description hereI am implementing a tableview with 2 xib files. But in some devices, it's crashing in height for rowAt: function (as per crash logs) Below is my code

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.row == 0 {
            return 340.0
        } else {
            return (contentHeights[indexPath.row] + 70)
        }
    }




  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: “customId1”, for: indexPath) as? customCell1
        if let details = Details{
            print(detail)
        }            
        cell?.selectionStyle = .none
        if cell == nil {
    print(“nil cell”)                
        }
        return cell!
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier:customId2, for: indexPath)  as? customCell2
        cell?.selectionStyle = .none

        if cell == nil {
            print(“nil cell”)
        }
        return cell!
    }
}

I am populating wkweview content in this cellforRow and then reloading the specific row on setting the tableview content.

Although it's working fine on all the devices but crashing on 2 devices out of 350 devices.

Any idea why is it happening?

2

There are 2 answers

0
Naveen Kommuri On

By doing this you can skip error but check why cell is coming to null. And also check in heightforRowAt contentHeights having that index or not

   if cell == nil {
        print(“nil cell”)
        return UITableViewCell()
    }
    return cell!
1
rantingmong On

Are you sure that the state (the dataSource) of your table view is synchronized with your actual data (not the dataSource)?

What I'm seeing from your code is that the computation for the row's height involves an array item access. Be sure that the table view's data is fully synchronized by calling the necessary methods to reload the table view state (such as beginUpdates-endUpdates and reloadData methods).

Also, do use assertion in your cellForRowAt method. It's useful for development purposes and avoids using print statements that continues execution of your code and can lead to undefined behavior.