UITableView internal inconsistency: _visibleRows and _visibleCells must be of same length

Viewed 5965

I'm currently trying to make a self sizing tableview (where the cells autosize to fit their inner view's autolayout).

One of the cells has a tableview in it. The idea is that the cell should be the size of the contentSize.height of the tableview.

I subclassed UITableView to accomplish this, and when the tableview loads, it looks great. However, this error gets spit out:

[Assert] UITableView internal inconsistency: _visibleRows and _visibleCells must be of same length. _visibleRows: {0, 4}; _visibleCells.count: 8, _visibleCells:

Additionally, when I try to scroll the tableview after this error there is very strange behavior and the tableview starts an infinite loop of layoutsubviews.

Any idea what this error means? I haven't been able to find anything substantial referencing this error on the internet. Thanks in advance!

4 Answers

I have a tableview with dynamic cell height. Cells are complete with about 30 layout constraints in there.

In the custom cell's -layoutSubviews, there was a call to tell the container tableView to setNeedsLayout.

Everything worked well until I rotated the device causing this error. I'm not hiding cell or anything like that. I presume this is another  bug.

Removing the [[self tableView] setNeedsLayout] is what fixed this issue for me.

If your view hierarchy of UITableView is something like this .

  • UITableView
    • UITableViewCells --- One cell has UITableView Follow this step to resize

1.Use tableview delegate method

  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          tableView.estimatedRowHeight = 80.0
        let modal = arrData[indexPath.row]
        if modal.isTableViewCell {
        let calculateTableViewHeight = 
        modal.arrInnerTableViewData.count * inner_cell_height
          return calculateTableViewHeight
        }
       return UITableViewAutomaticDimension
    }

2.Disable the scroll of Inner tableView

Related