【问题标题】:Show Header Footer and cell in single cell?在单个单元格中显示页眉页脚和单元格?
【发布时间】:2019-06-08 03:59:28
【问题描述】:

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

【问题讨论】:

  • 不,你不能因为页眉和页脚有特定的委托方法来显示视图或显示单元格
  • 您可以将包含所有页眉、页脚和内容的单元格设计为单独的视图,并动态显示/隐藏这些视图。
  • 但由于动态标签和动态视图高度,它非常困难..!!

标签: ios swift header tableview footer


【解决方案1】:

假设您的表格视图没有任何页眉和页脚。

  • 创建 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

【讨论】:

  • 但我的问题是如何在一张卡片中显示它们..?
  • @GurpreetSingh 我不认为你可以用一张卡做到这一点。
【解决方案2】:

不要将它们视为页脚和页眉。只是单元格的顶部、中间和底部部分。然后您可以构建一个包含这三个部分的复杂视图。 您可以将其中的 3 个实现为不同的自定义视图,单元格将只是所有这些视图的容器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 2020-07-30
    • 2011-02-10
    • 1970-01-01
    • 2015-08-31
    • 2020-03-21
    相关资源
    最近更新 更多