Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 606
Josh Avatar asked Nov 12 '17 02:11

Josh


3 Answers

I had the same issue and noticed that I was forcing reload the tableView during cell creation via a protocol fired by the cell.

The Problem:

    // ViewController: CellDelegate
        func onFirstUpdate(state: Bool){
          self.moreInfoHidden = state
          self.tableView.beginUpdates()
          self.tableView.endUpdates()
        }

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

                let cell = self.tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! MyTableViewCell
                cell.refresh(self.data)
                cell.delegate = self
                return cell
...

    // MyTableViewCell
    self.firstUpdate = true
    func refresh(_ data: MyItem){
       if firstUpdate{
          self.delegate.onFirstUpdate(true)
          self.firstUpdate = false
       }
    }

The Solution:

1. Removing tableview update as i understood it is not necessary in my case
           func onFirstUpdate(state: Bool){
              self.moreInfoHidden = state
              //self.tableView.beginUpdates()
              //self.tableView.endUpdates()
            }

2. (Not recommended because of degrading performance) Reload whole table properly again. 

       func onFirstUpdate(state: Bool){
          self.moreInfoHidden = state
          self.tableView.reloadData()
        }
like image 186
ugur Avatar answered Oct 28 '22 13:10

ugur


in my case it was due to editing tableview on background queue use your updating method like this DispatchQueue.main.async { [weak self] in self?.updateUI() }

like image 25
Hamed Nova Avatar answered Oct 28 '22 14:10

Hamed Nova


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.

like image 1
Martin-Gilles Lavoie Avatar answered Oct 28 '22 14:10

Martin-Gilles Lavoie