gpt4 book ai didi

ios - 默认的 UITableViewCell 高度对于文本来说太大了

转载 作者:行者123 更新时间:2023-11-28 15:05:28 25 4
gpt4 key购买 nike

我有一个带有默认单元格的 UITableView,我只是在其中添加了一个字符串:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!

cell.textLabel?.text = tasks[indexPath.row].name
cell.textLabel?.textColor = .white
cell.textLabel?.textAlignment = .center

return cell
}

我的问题是单元格对于一些普通文本来说太大了。这是一个屏幕截图,单元格具有蓝色背景:

enter image description here

我以前没有遇到过这个问题,当时我的数据源是 String 上的数组。在我从字符串更改为自定义对象后出现问题。不确定这是否相关。

我该如何解决这个问题?我尝试将行高设置为自定义值,但没有任何效果。

使用 TableView 代码编辑:

extension TasksViewController {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tasks.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")!

cell.textLabel?.text = tasks[indexPath.row].name
cell.textLabel?.textColor = .white
cell.textLabel?.textAlignment = .center

return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dummyTxt.textField.text = tasks[indexPath.row].name
dummyTxt.becomeFirstResponder()
dummyTxt.textField.becomeFirstResponder()
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (_, index) in
self.tasks.remove(at: index.row)
}

return [delete]
}
}

最佳答案

tableView 上设置:

tableView.estimatedRowHeight = 44
tableView.rowHeight = UITableViewAutomaticDimension

不要覆盖 heightForRowAt 委托(delegate)方法。

然后只需确保您注册为 "Cell" 的原型(prototype)单元格具有可用于计算其高度的适当约束。在您的情况下,请确保标签的顶部和底部 anchor 都被约束到单元格的顶部和底部 anchor 。

关于ios - 默认的 UITableViewCell 高度对于文本来说太大了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48542676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com