UITableViewCell 自动调整大小问题 Swift
UITableViewCell Auto Sizing Issue Swift
这是我在 xib
中设计的自定义 UITableViewCell
。所选元素是 UILabel
,需要根据文本量进行扩展。
在情节提要单元格中,每个元素都有一个恒定的高度和间距。
这是我在 UITableViewController
:
中的代码
class TableViewController: UITableViewController {
private var exampleContent = ["This is a short text.", "This is another text, and it is a little bit longer.", "Wow, this text is really very very long! I hope it can be read completely! Luckily, we are using automatic row height!, Wow, this text is really very very long! I hope it can be read completely! Luckily, we are using automatic row height!"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
tableView.estimatedRowHeight = 224.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("DescriptionOnlyCell", owner: self, options: nil)?.first as! DescriptionOnlyCell
cell.label.numberOfLines = 0
cell.label.text = exampleContent[indexPath.row]
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return exampleContent.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
代码的输出与故事板单元格完全相同,没有动态调整大小。
我在 stackover 上阅读了一些主题,其中大部分的解释与上面发布的代码相同。尽管我怀疑情节提要限制存在一些问题。任何提示都会很有帮助。
对所选标签执行以下操作:
- Set number of lines = 0
- Set lineBreak mode = WordWrap
- Set height constraint to >= 15
我依赖于您分享的所有 UI 组件的垂直间距已设置的信息。
我希望这可以解决您调整大小的问题。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
}
不要为标签设置恒定的高度值,而是为所有元素设置顶部和底部间距限制并移除高度限制或将它们的优先级设置得较低。
这是我在 xib
中设计的自定义 UITableViewCell
。所选元素是 UILabel
,需要根据文本量进行扩展。
在情节提要单元格中,每个元素都有一个恒定的高度和间距。
这是我在 UITableViewController
:
class TableViewController: UITableViewController {
private var exampleContent = ["This is a short text.", "This is another text, and it is a little bit longer.", "Wow, this text is really very very long! I hope it can be read completely! Luckily, we are using automatic row height!, Wow, this text is really very very long! I hope it can be read completely! Luckily, we are using automatic row height!"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.delegate = self
self.tableView.dataSource = self
tableView.estimatedRowHeight = 224.0
tableView.rowHeight = UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("DescriptionOnlyCell", owner: self, options: nil)?.first as! DescriptionOnlyCell
cell.label.numberOfLines = 0
cell.label.text = exampleContent[indexPath.row]
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return exampleContent.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
代码的输出与故事板单元格完全相同,没有动态调整大小。
我在 stackover 上阅读了一些主题,其中大部分的解释与上面发布的代码相同。尽管我怀疑情节提要限制存在一些问题。任何提示都会很有帮助。
对所选标签执行以下操作:
- Set number of lines = 0
- Set lineBreak mode = WordWrap
- Set height constraint to >= 15
我依赖于您分享的所有 UI 组件的垂直间距已设置的信息。
我希望这可以解决您调整大小的问题。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
}
不要为标签设置恒定的高度值,而是为所有元素设置顶部和底部间距限制并移除高度限制或将它们的优先级设置得较低。