ios - UITableView.sizeToFit() 只渲染第一个单元格

标签 ios swift uitableview uiscrollview reloaddata

我有一个标准的 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/

相关文章:

ios - 是否可以让iBeacon应用程序在没有GPS的情况下正常工作?

xcode4 : reliably detect the DerivedData directory of a project/workspace

ios - 将 Javascript 注入(inject) Webview - Swift

ios - UITableView 继续查看 Controller 不工作

ios - Swift 无法使具有标识符 Cell 的单元出列 - 必须为标识符注册一个 Nib 或一个类,或者在 Storyboard中连接一个原型(prototype)单元

ios - 重构 iOS ViewController 使其无需 Nib 即可工作

ios - 建议应用 iOS 地理围栏

swift - 使用带有 Swift2 的相机的黑色背景图像

swift - iOS 11 - Navigationbar 大标题自定义偏移量

ios - 为覆盖 ViewController 方法提供默认实现,但前提是 Controller 符合协议(protocol)