为什么单元格不随其内容调整大小?
Why is the cell not resizing with its content?
我已经阅读了所有可以在此处找到的类似问题并应用了所有建议的解决方案...none 其中有效。
table 视图是一个分组视图,textLabel
和 detailTextLabel
的行数在代码和故事板中都设置为 0,字符串数据由通过 "\n"
个字符创建的多行字符串组成...
将 tableView.estimatedRowHeight
设置为其他值而不是自动设置 43.5,然后按照其他一些问题的建议设置 tableView.rowHeight = UITableView.automaticDimension
绝对没有任何作用...或者如果我输入 100 之类的值,它会调整所有大小单元格而不仅仅是具有多行文本的单元格。
我不熟悉 table 视图中的此类行为:根据我的经验,只需将标签的行数设为 0...
为什么我会得到这个结果?
编辑:我也尝试实现 heightForRowAt
方法,传递所需的部分和行,但没有任何改变......我迷路了。
编辑 (2):删除了之前的代码片段并插入了 ViewController 源代码:我唯一能展示的与单元格相关的是这个,因为唯一的 属性 是一个实例填充 table 视图的自定义数据类型(它在这方面非常擅长)并且 viewDidLoad()
没有在其中添加我的任何内容。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
func configureTextLabels(withString labelText: String, and detailText: String) {
cell.textLabel?.text = labelText
cell.detailTextLabel?.text = detailText
}
if let country = detailCountry {
switch indexPath.section {
// insert 14 different cases here
case 14:
func populateRows() {
let regionalBlocs = country.regionalBlocs
for i in 0 ..< regionalBlocs.count {
switch indexPath.row {
case 0 + (i * 4): configureTextLabels(withString: "Name: ", and: "\(regionalBlocs[i].name)")
case 1 + (i * 4): configureTextLabels(withString: "\tAcronym: ", and: "\(regionalBlocs[i].acronym)")
case 2 + (i * 4):
var detailText = ""
let otherNames = regionalBlocs[i].otherNames
if !otherNames.isEmpty {
for name in otherNames {
if name == otherNames.last {
detailText += name
} else {
detailText += "\(name)\n"
}
}
configureTextLabels(withString: "\tOther names: ", and: detailText)
} else {
configureTextLabels(withString: "", and: "No other names found")
}
case 3 + (i * 4):
var detailText = ""
let otherAcronyms = regionalBlocs[0].otherAcronyms
if !otherAcronyms.isEmpty {
for acronym in otherAcronyms {
if acronym == otherAcronyms.last {
detailText += acronym
} else {
detailText += "\(acronym)\n"
}
}
configureTextLabels(withString: "\tOther acronyms: ", and: detailText)
} else {
configureTextLabels(withString: "", and: "No other acronym found")
}
default: break
}
}
}
let regionalBlocs = country.regionalBlocs
if regionalBlocs.isEmpty {
cell.textLabel?.text = ""
cell.detailTextLabel?.text = "No regional bloc data found for this country"
} else {
populateRows()
}
}
return cell
}
您可以在这里查看答案:Add multiple lines to detailTextLabel in UITableViewCell
但我建议您为这种情况创建自定义单元格,您的问题是 UIKit 的错误。
我已经阅读了所有可以在此处找到的类似问题并应用了所有建议的解决方案...none 其中有效。
table 视图是一个分组视图,textLabel
和 detailTextLabel
的行数在代码和故事板中都设置为 0,字符串数据由通过 "\n"
个字符创建的多行字符串组成...
将 tableView.estimatedRowHeight
设置为其他值而不是自动设置 43.5,然后按照其他一些问题的建议设置 tableView.rowHeight = UITableView.automaticDimension
绝对没有任何作用...或者如果我输入 100 之类的值,它会调整所有大小单元格而不仅仅是具有多行文本的单元格。
我不熟悉 table 视图中的此类行为:根据我的经验,只需将标签的行数设为 0...
为什么我会得到这个结果?
编辑:我也尝试实现 heightForRowAt
方法,传递所需的部分和行,但没有任何改变......我迷路了。
编辑 (2):删除了之前的代码片段并插入了 ViewController 源代码:我唯一能展示的与单元格相关的是这个,因为唯一的 属性 是一个实例填充 table 视图的自定义数据类型(它在这方面非常擅长)并且 viewDidLoad()
没有在其中添加我的任何内容。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.detailTextLabel?.numberOfLines = 0
func configureTextLabels(withString labelText: String, and detailText: String) {
cell.textLabel?.text = labelText
cell.detailTextLabel?.text = detailText
}
if let country = detailCountry {
switch indexPath.section {
// insert 14 different cases here
case 14:
func populateRows() {
let regionalBlocs = country.regionalBlocs
for i in 0 ..< regionalBlocs.count {
switch indexPath.row {
case 0 + (i * 4): configureTextLabels(withString: "Name: ", and: "\(regionalBlocs[i].name)")
case 1 + (i * 4): configureTextLabels(withString: "\tAcronym: ", and: "\(regionalBlocs[i].acronym)")
case 2 + (i * 4):
var detailText = ""
let otherNames = regionalBlocs[i].otherNames
if !otherNames.isEmpty {
for name in otherNames {
if name == otherNames.last {
detailText += name
} else {
detailText += "\(name)\n"
}
}
configureTextLabels(withString: "\tOther names: ", and: detailText)
} else {
configureTextLabels(withString: "", and: "No other names found")
}
case 3 + (i * 4):
var detailText = ""
let otherAcronyms = regionalBlocs[0].otherAcronyms
if !otherAcronyms.isEmpty {
for acronym in otherAcronyms {
if acronym == otherAcronyms.last {
detailText += acronym
} else {
detailText += "\(acronym)\n"
}
}
configureTextLabels(withString: "\tOther acronyms: ", and: detailText)
} else {
configureTextLabels(withString: "", and: "No other acronym found")
}
default: break
}
}
}
let regionalBlocs = country.regionalBlocs
if regionalBlocs.isEmpty {
cell.textLabel?.text = ""
cell.detailTextLabel?.text = "No regional bloc data found for this country"
} else {
populateRows()
}
}
return cell
}
您可以在这里查看答案:Add multiple lines to detailTextLabel in UITableViewCell
但我建议您为这种情况创建自定义单元格,您的问题是 UIKit 的错误。