UITableView.sizeToFit() 只渲染第一个单元格
UITableView.sizeToFit() causes only first cell to be rendered
我有一个标准的 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;
并且您应该在主线程上执行此操作!
我有一个标准的 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;
并且您应该在主线程上执行此操作!