在我发布这篇文章之前,我已经阅读了一些文章,例如 this或 this ,但所有该示例始终使用动态数据。我想知道如何在静态数据中实现可扩展的 TableView 。我认为这更简单,但我不知道。我的代码片段:
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 个部分。我想让第一部分可以扩展,但带有静态表、单元格和静态数据。
最佳答案
Static cells: Use static content to design the overall layout of the table, including the total number of cells. A table view with static content has a fixed set of cells that you can configure at design time. You can also configure other static data elements such as section headers. Use static cells when a table does not change its layout, regardless of the specific information it displays
在您的场景中,由于您使用的是动态数据源,因此您必须使 TableView 动态
关于Swift:可扩展的静态 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448167/