UITableViewCell具有多个动态元素的动态高度

时间:2017-04-04 20:23:26

标签: ios height tableviewcell

我有一个带有元素的tableViewCell(c - 表示约束):

  • 标签:固定c.height(不重要)
  • Label1:多行,自动换行,c.height> = 20
  • Label2:multiline,自动换行,c.height> = 16
  • ImageView:aspectFit,c.height> = 100.高度插座
  • 标签:固定c.height(不重要)

TableView设置了estimatedH = 30和rowH = Automatic

压缩/拥抱优先级未被更改 - 默认,bcs所有元素具有相同的优先级

在cellForRow中的

我设置了Label1和Label2文本(相应的2行和3行)。我也为完成后的imageView调用SDWebImage方法sd_setImage。完成后我按照公式

设置imageView高度
  

newHeight =(cell.newsImageView.bounds.width / image.size.width)* image.size.height

但结果是,我的标签显示最多2行文字,而imageView的高度不正确。在tableView重新加载(pull-to-refresh)或新的dequed单元格之后都可以,但是对于可见 - 不好。

我已经尝试使用SDWebImagePrefetcher预取图像并在完成时重新加载表格,但这不会帮助

如何解决此问题?

更新05.04.17

这是关于约束冲突的日志堆栈。也许它可以帮助分析问题。

(
"<NSLayoutConstraint:0x797a1ec0 V:[UILabel:0x7b06b990'22.03.2017 '(16)]>",
"<NSLayoutConstraint:0x797c7250 V:[UIImageView:0x797e6560(214.2)]>",
"<NSLayoutConstraint:0x797ca430 V:[UILabel:0x797c7900'\U0427\U0418\U0422\U0410\U0422\U0418 \U041d\U041e\U0412\U0418\U041d\U0423'(20)]>",
"<NSLayoutConstraint:0x797d7ac0 V:|-(16)-[UILabel:0x7b06b990'22.03.2017 ']   (Names: '|':UIView:0x7b0810d0 )>",
"<NSLayoutConstraint:0x797d2ac0 V:[UILabel:0x7b06b990'22.03.2017 ']-(0)-[UILabel:0x797c40a0'\U0406\U043d\U0444\U043e\U0440\U043c\U0430\U0446\U0456\U044f \U0434\U043b\U044f \U0430\U0431\U043e\U043d\U0435\U043d\U0442\U0456\U0432 ...']>",
"<NSLayoutConstraint:0x797d2b80 V:[UILabel:0x797c40a0'\U0406\U043d\U0444\U043e\U0440\U043c\U0430\U0446\U0456\U044f \U0434\U043b\U044f \U0430\U0431\U043e\U043d\U0435\U043d\U0442\U0456\U0432 ...']-(0)-[UILabel:0x797c9090'\U0417 1 \U043a\U0432\U0456\U0442\U043d\U044f 2017 \U0440\U043e\U043a\U0443 \U043f\U0440\U043e\U0432...']>",
"<NSLayoutConstraint:0x797d7c70 V:[UILabel:0x797c9090'\U0417 1 \U043a\U0432\U0456\U0442\U043d\U044f 2017 \U0440\U043e\U043a\U0443 \U043f\U0440\U043e\U0432...']-(10)-[UIImageView:0x797e6560]>",
"<NSLayoutConstraint:0x797d7d00 V:[UIImageView:0x797e6560]-(10)-[UILabel:0x797c7900'\U0427\U0418\U0422\U0410\U0422\U0418 \U041d\U041e\U0412\U0418\U041d\U0423']>",
"<NSLayoutConstraint:0x797d7d30 V:[UILabel:0x797c7900'\U0427\U0418\U0422\U0410\U0422\U0418 \U041d\U041e\U0412\U0418\U041d\U0423']-(10)-|   (Names: '|':UIView:0x7b0810d0 )>",
"<NSLayoutConstraint:0x797d8160 V:[UIView:0x7b0810d0]-(5)-|   (Names: '|':UITableViewCellContentView:0x7b151eb0 )>",
"<NSLayoutConstraint:0x797d8190 V:|-(5)-[UIView:0x7b0810d0]   (Names: '|':UITableViewCellContentView:0x7b151eb0 )>",
"<NSLayoutConstraint:0x797dacb0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7b151eb0(274)]>"

更新:Sample project

1 个答案:

答案 0 :(得分:0)

您正在设置estimatedH = 30,但实际上有更多内容

20 + 16 + 100 = 136这远远超过您的估计

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 150

如果要在下载数据后更新特定单元格,则可以调用更新表视图

 self.tableview.beginUpdates()
 tableview.reloadRows(at: [indexPath], with:.bottom)
 self.tableview.endUpdates()

reloadRows有一个枚举选项,您可以使用点符号访问这些选项。我选择.bottom但您可以从符合您需要的枚举中选择

更新:Sample Project

相关问题