Swift: 可扩展的静态 TableView

Swift: Expandable Static TableView

在我 post 之前,我已经阅读了一些文章,例如 this or 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 个部分。我想让第一部分可以扩展,但带有静态 table、单元格和静态数据。

来自Apple's developer site

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

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

我解决了。我阅读了一些使用 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)
    }
}