【问题标题】:Swift: Expandable Static TableViewSwift:可扩展的静态 TableView
【发布时间】:2017-11-10 21:41:00
【问题描述】:

在发布此之前,我已经阅读了一些类似 this 或 this 的文章,但所有这些示例都始终使用动态数据。我想知道如何在静态数据中实现可扩展的表格视图。我认为这更简单,但我不知道。我的 sn-p 代码:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (indexPath.section == 1) {
        if indexPath.row == self.department {
            cell.accessoryType = .checkmark
        } else {
            cell.accessoryType = .none
        }
    }

    if indexPath.section == 0 {
        if indexPath.row == 2 {
            datePickerFrom.isHidden = true
        }
        else if indexPath.row == 4 {
            datePickerUntil.isHidden = true
        }
    }

}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: false)

    if indexPath.section == 1 {
        let lastDepartment = self.department
        self.department = indexPath.row

        tableView.reloadRows(at: [indexPath, IndexPath.init(row: lastDepartment, section: 1)], with: .none)
    }

    if indexPath.section == 0 {
        if indexPath.row == 2 {
            datePickerFrom.isHidden = false
        }
        else if indexPath.row == 4 {
            datePickerUntil.isHidden = false
        }

        tableView.reloadData()
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    var count = 0
    if section == 0 {


    } else if section == 1 {
        count = 2
    } else if section == 2 || section == 3 {
        count = 1
    }

    return count
}

假设我有 4 个部分。我想让第一部分可扩展,但包含静态表格、单元格和静态数据。

【问题讨论】:

    标签: swift uitableview expand


    【解决方案1】:

    来自Apple's developer site:

    静态单元格:使用静态内容来设计整体布局 表格,包括单元格的总数。静态表格视图 content 有一组固定的单元格,您可以在设计时对其进行配置 时间。您还可以配置其他静态数据元素,例如 节标题。当表格不更改其内容时使用静态单元格 布局,不管它显示的具体信息是什么

    在您的场景中,由于您使用的是动态数据源,因此您必须将表格视图设为dynamic

    【讨论】:

      【解决方案2】:

      我解决了。我阅读了一些使用Bool 值并设置高度来管理展开和折叠单元格的示例。这是我的最终代码:

      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
          tableView.deselectRow(at: indexPath, animated: false)
      
          if indexPath.section == 1 {
              let lastDepartment = self.department
              self.department = indexPath.row
      
              tableView.reloadRows(at: [indexPath, IndexPath.init(row: lastDepartment, section: 1)], with: .none)
          }
      
          if indexPath.section == 0 && indexPath.row == 1 {
      
              if !isExpandToDate {
                  if isExpandFromDate {
                      isExpandFromDate = !isExpandFromDate
                      isExpandToDate = !isExpandToDate
                  } else {
                      isExpandFromDate = !isExpandFromDate
                  }
              } else {
                  if isExpandFromDate {
                      isExpandFromDate = !isExpandToDate
                  } else {
                      isExpandFromDate = !isExpandFromDate
                  }
              }
          }
      
          if indexPath.section == 0 && indexPath.row == 3 {
      
              if !isExpandFromDate {
                  if isExpandToDate {
                      isExpandToDate = !isExpandToDate
                      isExpandFromDate = !isExpandFromDate
                  } else {
                      isExpandToDate = !isExpandToDate
                  }
              } else {
                  if isExpandToDate {
                      isExpandToDate = !isExpandFromDate
                  } else {
                      isExpandToDate = !isExpandToDate
                  }
              }
          }
      
          tableView.beginUpdates()
          tableView.endUpdates()
      }
      
      
      override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
          if isExpandFromDate && indexPath.section == 0 && indexPath.row == 2 {
              return 0
          }
          else if isExpandToDate && indexPath.section == 0 && indexPath.row == 4 {
              return 0
          }
          else {
              return super.tableView(tableView, heightForRowAt: indexPath)
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        • 1970-01-01
        相关资源
        最近更新 更多