UITableViewCell 内的 UITableView 未调整单元格高度
UITableView inside UITableViewCell not resizing the cell height
我创建了一个 tableView,它有两个单元格:第一个只是带有一些标签的 tableViewCell,是您在顶部看到的那个;第二个单元格是嵌入式 table 视图。
我遇到的问题是具有嵌入式 table 视图的单元格未根据嵌入式 table 视图大小调整大小。在此屏幕截图中,嵌入式 table 视图为绿色,红色为嵌入式 table 视图的单元格。有一些红细胞被隐藏了,因为细胞没有像预期的那样膨胀。
具有嵌入式 table 视图的单元格在此处定义:
嵌入式 table 视图的单元格是这样的:
作为一般规则,将 table 视图嵌入 table 视图单元格是一个糟糕的设计,原因有很多。
- Table 视图没有固有高度...因此无论您添加多少个单元格,它都不会设置自己的高度。有很多解决方法,但是...
- 根据单元格数量更改 table 视图的高度会破坏“可重用”单元格设计。 Table 视图将其用于内存管理。
- 您最终在单元格的内容中滚动,而 table 包含该单元格的视图也想要滚动。
- 每个 table 视图需要单独的
dataSource
和 delegate
class。
除非您的设计 比您展示的要复杂得多,或者您有其他令人信服的理由来嵌入 table 在单元格中查看,使用这种方法效果会更好:
- 一个 table 视图
- 两个电池 classes - 我称它们为白色和红色以匹配您的图片
- 一个
dataSource
- returns 一个 WhiteCell
如果 indexPath.row == 0
和 returns 一个 RedCell
对于其余的行。
下面是该方法的概述:
class WhiteCell: UITableViewCell {
func configureCell(_ strings: [String]) -> Void {
// configure the labels for this cell
}
}
class RedCell: UITableViewCell {
func configureCell(_ strings: [String]) -> Void {
// configure the labels for this cell
}
}
class MyTableViewController: UITableViewController {
let whiteData: [String] = [
"Line 1",
"Line 2",
"Line 3",
"Line 4",
// etc...
]
let redData: [[String]] = [
[ "A string 1", "A string 2", "A string 3", ],
[ "B string 1", "B string 2", "B string 3", ],
[ "C string 1", "C string 2", "C string 3", ],
[ "D string 1", "D string 2", "D string 3", ],
// etc...
]
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// number of red rows + the white row
return redData.count + 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "WhiteCell", for: indexPath) as! WhiteCell
cell.configureCell(whiteData)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "RedCell", for: indexPath) as! RedCell
// because the red cells start at .row == 1, and the data array is zero-based,
// we use [indexPath.row - 1]
cell.configureCell(redData[indexPath.row - 1])
return cell
}
}
我创建了一个 tableView,它有两个单元格:第一个只是带有一些标签的 tableViewCell,是您在顶部看到的那个;第二个单元格是嵌入式 table 视图。
我遇到的问题是具有嵌入式 table 视图的单元格未根据嵌入式 table 视图大小调整大小。在此屏幕截图中,嵌入式 table 视图为绿色,红色为嵌入式 table 视图的单元格。有一些红细胞被隐藏了,因为细胞没有像预期的那样膨胀。
具有嵌入式 table 视图的单元格在此处定义:
嵌入式 table 视图的单元格是这样的:
作为一般规则,将 table 视图嵌入 table 视图单元格是一个糟糕的设计,原因有很多。
- Table 视图没有固有高度...因此无论您添加多少个单元格,它都不会设置自己的高度。有很多解决方法,但是...
- 根据单元格数量更改 table 视图的高度会破坏“可重用”单元格设计。 Table 视图将其用于内存管理。
- 您最终在单元格的内容中滚动,而 table 包含该单元格的视图也想要滚动。
- 每个 table 视图需要单独的
dataSource
和delegate
class。
除非您的设计 比您展示的要复杂得多,或者您有其他令人信服的理由来嵌入 table 在单元格中查看,使用这种方法效果会更好:
- 一个 table 视图
- 两个电池 classes - 我称它们为白色和红色以匹配您的图片
- 一个
dataSource
- returns 一个WhiteCell
如果indexPath.row == 0
和 returns 一个RedCell
对于其余的行。
下面是该方法的概述:
class WhiteCell: UITableViewCell {
func configureCell(_ strings: [String]) -> Void {
// configure the labels for this cell
}
}
class RedCell: UITableViewCell {
func configureCell(_ strings: [String]) -> Void {
// configure the labels for this cell
}
}
class MyTableViewController: UITableViewController {
let whiteData: [String] = [
"Line 1",
"Line 2",
"Line 3",
"Line 4",
// etc...
]
let redData: [[String]] = [
[ "A string 1", "A string 2", "A string 3", ],
[ "B string 1", "B string 2", "B string 3", ],
[ "C string 1", "C string 2", "C string 3", ],
[ "D string 1", "D string 2", "D string 3", ],
// etc...
]
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// number of red rows + the white row
return redData.count + 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "WhiteCell", for: indexPath) as! WhiteCell
cell.configureCell(whiteData)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "RedCell", for: indexPath) as! RedCell
// because the red cells start at .row == 1, and the data array is zero-based,
// we use [indexPath.row - 1]
cell.configureCell(redData[indexPath.row - 1])
return cell
}
}