首页 > 解决方案 > 在单个单元格中显示页眉页脚和单元格?

问题描述

截图做任务

请建议我可以在带有单元格标题单元格页脚和单元格的单卡内执行此操作。

标签: iosswiftheadertableviewfooter

解决方案


考虑到您的表格视图没有任何页眉和页脚。

  • 创建 3 种类型的单元格,即答案单元格、问题单元格和解决方案单元格。
  • 根据您的 UI 要求设计您的单元格。
  • 为问题、答案和解决方案单元格创建数据源。

在 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


推荐阅读