【发布时间】:2019-06-08 03:59:28
【问题描述】:
【问题讨论】:
-
不,你不能因为页眉和页脚有特定的委托方法来显示视图或显示单元格
-
您可以将包含所有页眉、页脚和内容的单元格设计为单独的视图,并动态显示/隐藏这些视图。
-
但由于动态标签和动态视图高度,它非常困难..!!
标签: ios swift header tableview footer
【问题讨论】:
标签: ios swift header tableview footer
假设您的表格视图没有任何页眉和页脚。
在TableView的CellForRowAtIndexPath的Delegate方法中实现如下代码。
enum CellType : String {
case answer
case solution
case question
}
func tableView(_ tableView: UITableView,
cellForItemAt indexPath: IndexPath) -> UITableViewCell {
let cellType = arrayDataSource[indexPath.item]
switch cellType {
case .answer:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "answerCell",
for: indexPath) as! AnswerCell
return cell
case .solution:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "solutionCell",
for: indexPath) as! SolutionCell
return cell
case .question:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "questionCell",
for: indexPath) as! QuestionCell
return cell
}
}
根据单元格类型创建数据源。
在表格视图的委托方法中管理行的高度。使用 iOS 9 中引入的 UITableViewAutomaticDimension。
以下链接了解自动行高:
https://www.raywenderlich.com/8549-self-sizing-table-view-cells
【讨论】:
不要将它们视为页脚和页眉。只是单元格的顶部、中间和底部部分。然后您可以构建一个包含这三个部分的复杂视图。 您可以将其中的 3 个实现为不同的自定义视图,单元格将只是所有这些视图的容器。
【讨论】: