【问题标题】:Why did I get empty cells in UITableView?为什么我在 UITableView 中得到空单元格?
【发布时间】:2021-08-15 11:01:45
【问题描述】:

我将 UITableView 放在 UIScrollView > View > StackView 中。

我像往常一样在 View Controller 中使用 numberOfRowsInSection 和 cellForRowAt 实现了表格视图。这是我的扩展。

extension OrderViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    print("DEBUG: orderItems.count - \(orderItems.count)")
    return orderItems.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: OrderCell.id, for: indexPath) as! OrderCell
    cell.configure(orderItems[indexPath.row])
    return cell
} 

}

但我的模拟器实际上显示的是空单元格。我已经检查了 orderItems 数组只有 2 个成员。

我猜这是因为自动布局或堆栈视图。在堆栈视图中,我为除表格视图之外的每个视图提供了高度限制,因为它必须具有动态单元格编号。

我不知道我必须改变什么。

table view's attribute

【问题讨论】:

  • 我认为你需要给tableview设置高度约束,然后在重新加载tableview后使用tableview的数据更改高度约束。

标签: ios swift autolayout


【解决方案1】:

这是因为您的 tableview 具有与您的视图相关的完整高度。只需添加 viewDidLoad

yourTableView.tableFooterView = UIView()

但是如果你想要你的 tableview 的动态高度,那就这样做吧;

  1. 为您的 tableview 设置高度限制
  2. 为此约束创建一个 IBOutlet

重新加载表格视图后

heightConstraint.constant = yourRowHeight * arrayCount
self.view.layoutIfNeeded()

【讨论】:

  • 对不起,但我添加了它,我得到了填充的单元格和它们下面的大空白区域 lmao...
  • 我回答了隐藏空闲单元格的简单方法如果您想要表格视图的动态高度,那么您可以这样做 - 为表格视图提供高度约束并为此约束创建一个 IBOutlet - 然后在你之后重新加载您的表格视图 heightconstraint.contant = yourrowheight * arraycount self.view.layoutIfNeeded()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
相关资源
最近更新 更多