首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UITableView中,在cellforRowAt之后调用heightForRowAt

在UITableView中,在cellforRowAt之后调用heightForRowAt
EN

Stack Overflow用户
提问于 2017-11-03 19:27:43
回答 1查看 373关注 0票数 1

每当我重新加载tableView时,在cellForRowAt之后都会调用heightForRowAt。它遵循的顺序是:

代码语言:javascript
复制
 numberofsections -> numberOfRowsInSection -> cellForRowAt -> heightForRow

我重写了numberOfSections, numberOfRowsInSection, viewForHeaderInSection方法。这些对调用heightForRowcellForRowAt的顺序有影响吗?

我使用的是Xcode9.1,也就是运行在iOS 11上的Swift 3。

EN

回答 1

Stack Overflow用户

发布于 2017-11-04 01:42:40

这里有两个选项:

  1. 以定义单元格大小的方式对单元格进行contentView。这意味着你的布局不应该是雄心勃勃的vertically.
  2. Calculate它的大小手动。求子视图之间的偏移(高度)之和。这可能是一个计算label / textView大小的问题(我使用text扩展来计算文本大小)。关注textView.contentInsets!你也需要记住它才能得到正确的尺寸

扩展字符串{ func height(withConstrainedWidth width: CGFloat,font: UIFont) -> CGFloat { let constraintRect = CGSize(width: width,height:.greatestFiniteMagnitude) let boundingBox = self.boundingRect(with: constraintRect,options:.usesLineFragmentOrigin,attributes: NSAttributedStringKey.font: font,context: nil) return ceil(boundingBox.height) } func width(withConstraintedHeight height: CGFloat,font: UIFont) -> CGFloat { let constraintRect = CGSize(width:.greatestFiniteMagnitude,height: height)让boundingBox = self.boundingRect(with: constraintRect,options:.usesLineFragmentOrigin,attributes: NSAttributedStringKey.font: font,context: nil) return ceil(boundingBox.width) } }

希望能对你有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47094697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档