【发布时间】:2019-10-12 02:25:52
【问题描述】:
有 2 个原型单元。每个都需要不同的大小。所以我有单元格和单元格1。单元格应为 40,单元格应为 75。
我尝试使用 heightForRowAt - 发现这是在 cellForRowAt 之前调用的
我厌倦了在故事板上为每个单元格设置高度
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "starshipCell", for: indexPath)
let cell1 = tableView.dequeueReusableCell(withIdentifier: "starshipCell1", for: indexPath) as! SectionTableViewCell
switch indexPath {
case [4,0]:
cell1.sectionLbl.text = "Armor".lowercased()
cell1.detailTextLabel?.text = "Test"
return cell1
case [4,1]:
cell.textLabel?.text = "Defensive Countermeasures"
return cell
case [4,2]:
cell.textLabel?.text = "Shields"
return cell
case [11, 0]:
cell.textLabel?.text = "Forward Arc"
return cell
case [11, 1]:
cell.textLabel?.text = "Port Arc"
return cell
case [11, 2]:
cell.textLabel?.text = "Starboard Arc"
return cell
case [11, 3]:
cell.textLabel?.text = "Aft Arc"
return cell
case [11, 4]:
cell.textLabel?.text = "Turret"
return cell
default:
return cell
}
// return cell
}
'试图为同一索引路径使多个单元格出列,这是不允许的。如果您确实需要比表格视图请求更多的单元格出列,请使用 -dequeueReusableCellWithIdentifier: 方法(没有索引路径)。单元标识符:starshipCell1,索引路径:{length = 2, path = 0 - 0}'
所以上面的工作完美。我只需要调整这些单元格的行高。现在代码不是 100% 完成的。以上所有案例都将更改并更新为cell1。错误仅在我使用 heightForRowAt 时出现
【问题讨论】:
-
您是否在两个原型单元格中都使用自动布局?
-
我还没有添加。我刚刚发现我可以有多个原型单元。所以我正在测试一个概念。现在我注意到在故事板中,如果我改变单元格本身的高度,那么在构建它时它不会改变它。但是,如果我选择表格并更改行高,它将应用所需的高度。这里的问题是我需要不同的高度。
-
您可以在两个原型单元格中使用自动布局。并在 heightForRow 方法中使用 UITableView.automaticDimension 。你会得到 2 个不同高度的单元格。检查这个thomashanning.com/uitableview-automatic-row-height
-
那个教程没有帮助。我没有将推送到第二行的文本。
-
你想什么时候用cell,什么时候用cell1?
标签: swift uitableview height cell