就像标题说的那样,我正在寻找一种方法来更改表格视图中每个单元格的大小。我有2个问题:
1)如何在表格视图中以相同的数量更改所有单元格的大小?
2)是否可以根据特定单元格中文本的长度自动调整单元格的大小?
我知道之前已经有类似的问题,但是由于它们已经过时了一段时间,因此我无法正确实现它们的答案。感谢您的帮助,谢谢!
答案 0 :(得分:0)
1)要更改同一类型的所有单元格的大小,首先需要创建CellType
,这是如何执行此操作的说明。
https://stackoverflow.com/a/51979506/8417137
或者您可以检查indexPath.row,但这不是很酷:)
比这里:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
default:
return defaultCellHeight
}
}
2)是的,有可能。您创建CustomCell
。在您的CustomCell
中,您应该
在ContentView
内设置textField或标签。您的文字视图将使您的单元格更加舒展。
重要的事情。在上面的方法中,您必须返回类似的特殊高度。
return UITableViewAutomaticDimension
例如,您的代码将如下所示。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
// Your stretching cell
case textCellType:
return UITableViewAutomaticDimension
}
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
switch cellType {
case firstType:
return firstCellTypeHeight
case secondType:
return secondCellTypeHeight
// Your stretching cell
case textCellType:
return UITableViewAutomaticDimension
}
}