我有一个标准的 UITableView,定义如下
self.productTable = UITableView(frame: CGRect(x:0, y:0, width: productsView.bounds.width, height: productsView.bounds.height), style: UITableViewStyle.plain)
self.productTable.delegate = self
self.productTable.dataSource = self
self.productTable.isScrollEnabled = false
self.productTable.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
我在 UIScrollView 中有这个,因此我也禁用滚动。当我获得一些新数据时,我会执行以下操作
self.productTable.reloadData()
self.productTable.sizeToFit()
当我执行这个时,我的 cellForRowAt indexPath 函数只被调用一次,导致只有一个单元格被渲染。
如果我删除 sizeToFit 调用,所有行都将毫无问题地输出。
为什么会这样?我以为 reloadData 是同步的?
谢谢
最佳答案
在您重新加载数据后尝试使用下面的代码而不是“sizeToFit”,
CGRect newFrame = self.yourTableView.frame;
newFrame.size.height = self.yourTableView.contentSize.height;
self.yourTableView.frame = newFrame;
而且您应该在主线程上执行此操作!
关于ios - UITableView.sizeToFit() 只渲染第一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41536868/