我目前正在尝试制作自行调整大小的表格视图(细胞自动调整大小以适应其内部视图的自动布局)。
其中一个单元格中有一个tableview。这个想法是单元格应该是tableview的contentSize.height的大小。
我将UITableView子类化为完成此任务,当tableview加载时,它看起来很棒。但是,这个错误会被吐出:
[Assert] UITableView internal inconsistency: _visibleRows and _visibleCells must be of same length. _visibleRows: {0, 4}; _visibleCells.count: 8, _visibleCells:
此外,当我尝试在此错误后滚动tableview时,会出现非常奇怪的行为,并且tableview会启动layoutsubviews的无限循环。
知道这个错误意味着什么吗?我还没有能够在互联网上找到任何可以引用此错误的实质内容。提前谢谢!
答案 0 :(得分:5)
在我的情况下,这是由于在后台队列上编辑tableview
使用这样的更新方法
DispatchQueue.main.async { [weak self] in
self?.updateUI()
}
答案 1 :(得分:1)
我有同样的问题,并注意到我在通过单元格触发的协议创建单元格期间强制重新加载tableView。
问题:
// 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
}
}
解决方案:
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()
}
答案 2 :(得分:0)
如果UITableView的视图层次结构是这样的。
1.使用tableview委托方法
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.禁用Inner tableView的滚动
答案 3 :(得分:0)
我有一个带有动态单元格高度的表格视图。单元格完整,其中大约有30个布局约束。
在自定义单元格的-layoutSubviews中,有一个调用将容器tableView告知setNeedsLayout。
一切正常,直到我旋转设备导致此错误。我没有躲藏牢房之类的东西。我想这是另一个虫。
为我解决此问题的原因是删除[[self tableView] setNeedsLayout]。