【发布时间】:2018-05-22 20:02:59
【问题描述】:
我的动态UITableViewCell 有一些身高问题(缝合下图)。有些单元格的高度正确,有些则没有,当我拖动 tableView 时,有些单元格变得正确,有些则不正确。
我正在使用此代码使单元格的高度变为动态并在viewDidAppear 和viewDidLoad 中重新加载它。
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