【问题标题】:Dynamic height for UITableViewCell not working correctlyUITableViewCell 的动态高度无法正常工作
【发布时间】:2018-05-22 20:02:59
【问题描述】:

我的动态UITableViewCell 有一些身高问题(缝合下图)。有些单元格的高度正确,有些则没有,当我拖动 tableView 时,有些单元格变得正确,有些则不正确。

我正在使用此代码使单元格的高度变为动态并在viewDidAppearviewDidLoad 中重新加载它。

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = tableView.rowHeight

如前所述,单元格有时正确,有时不正确。还有其他方法可以做到还是我做错了什么?我尝试了许多不同的解决方案,都提到了 here 以及 StackOverflow 和其他网站上的其他建议。

感谢所有帮助!

编辑!

表格视图

extension ChatVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return groupMessages.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "groupFeedCell", for: indexPath) as? GroupFeedCell else { return UITableViewCell() }

        let message = groupMessages[indexPath.row]

            DataService.instance.getUsername(forUID: message.senderId, handler: { (name) in
                cell.configureCell(name: name, content: message.content)
            })

        cell.layoutSubviews()
        cell.layoutIfNeeded()

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return UITableViewAutomaticDimension
    }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 66
    }
}

细胞

@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var contentLbl: UILabel!

func configureCell(name: String, content: String) {
    self.nameLbl.text = name //email
    self.contentLbl.text = content
}

【问题讨论】:

  • 你把标签行数改成0了吗?
  • 是的,我已经做到了
  • 我希望您在单元出列时不提供随机数据
  • 不,我要从 firebase 中删除所有数据。
  • 在这里查看我的答案,它应该会有所帮助。 stackoverflow.com/questions/42717173/…。您需要确保约束覆盖 contentView 的所有方面,尤其是 nameLbl

标签: ios swift uitableview dynamic swift4


【解决方案1】:

对于动态tableViewCell

1- 使用行高的初始值设置这 2 行,以帮助自动布局绘制它(从 nib 文件中的当前单元格高度获取)

tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = number;

2- 不要实现这个函数 heightForRowAtIndexPath 。或者实现它并返回这个

return UITableViewDynamicHeight;

3- 确保单元格 nib 文件或情节提要中的所有约束从上到下正确挂钩。

4- 在 cellForRowAtIndexPath 行之前 retrun cell 插入该

[cell layoutSubviews];
[cell layoutIfneeded];

5- 在模拟器中测试一些版本,例如 ios 8,这也是 viewController 调用中的一个错误

[tableView LayouSubviews];

在 viewdidLayoutSubViews 函数中重新正确重新布局

6- 使您想要包装的任何 UILabellines 属性 = 0 并将其挂钩 leadingtrailing superView 的约束

【讨论】:

    【解决方案2】:

    您正面临此问题,因为您的标签内容来自异步函数。

    单元格使用其内容动态计算其高度。当您的异步请求返回时,它已经完成了工作,不会重新计算和调整大小。

    您需要发出这些请求、缓存/存储结果并根据需要重新加载单元格。通常在聊天中只会有几个用户加载用户名。您还可以尝试在显示聊天之前预加载此数据。

    您可以通过创建一组随机用户名和消息(示例数据)并立即将其添加到单元格中来快速确认这一点。

    【讨论】:

      【解决方案3】:

      我猜问题是异步函数。所以你应该试试

      第一步:创建名称数组

      var names: [String?] = Array<String>.init(repeating: nil, count: groupMessages.count)
      

      第 2 步:替换

       DataService.instance.getUsername(forUID: message.senderId, handler: { (name) in
                      cell.configureCell(name: name, content: message.content)
                  })
      

       if names[indexPath.row] == nil {
                  DataService.instance.getUsername(forUID: message.senderId, handler: { (name) in
                      names[indexPath.row] = name
      
                      tableView.reloadRows(at: [indexPath], with: .automatic)
                  })
              } else {
                  cell.configureCell(name: name, content: message.content)
      
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-16
        • 1970-01-01
        • 2014-06-28
        • 2018-12-17
        相关资源
        最近更新 更多