UITableViewCell的动态高度无法正常工作

时间:2017-12-08 14:47:19

标签: ios swift uitableview dynamic swift4

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

我使用此代码将单元格的高度设置为动态,并在viewDidAppearviewDidLoad中重新加载。

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

如上所述,细胞有时是正确的,有时不是。还有另一种方法可以做到这一点,还是我做错了什么?我尝试了许多不同的解决方案,所有提到here以及StackOverflow和其他网站的其他建议。

enter image description here

enter image description here

我感谢所有人的帮助!

编辑!

的TableView

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
}

enter image description here

3 个答案:

答案 0 :(得分:1)

对于动态 tableViewCell

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

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

2-不要实现此功能 heightForRowAtIndexPath 。或实现它并返回此

return UITableViewDynamicHeight;

3-确保单元格nib文件或storyboard中的所有约束从上到下正确连接。

后退单元格之前的 cellForRowAtIndexPath 中的

4-

[cell layoutSubviews];
[cell layoutIfneeded];

5-在模拟器中测试某些版本,比如ios 8,它也是viewController调用中的一个bug

[tableView LayouSubviews];

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

6 - 要包装的任何 UILabel 属性= 0并挂钩它前导尾随 constarints到superView

答案 1 :(得分:1)

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

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

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

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

答案 2 :(得分:0)

我猜问题是异步功能。所以你应该试试

第1步:创建名称数组

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)

        }