Swift:可扩展的静态 TableView

标签 swift uitableview expand

在我发布这篇文章之前,我已经阅读了一些文章,例如 thisthis ,但所有该示例始终使用动态数据。我想知道如何在静态数据中实现可扩展的 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 个部分。我想让第一部分可以扩展,但带有静态表、单元格和静态数据。

最佳答案

来自 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

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

关于Swift:可扩展的静态 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44448167/

相关文章:

ios - 无法向数组添加元素

ios - 函数应该将用户添加到 Firebase 中其他 friend 节点的列表中,但它返回 nil

objective-c - 在 Xcode 变量 View 中更改变量描述

ios - 停止 UITableView 消耗触摸事件,以便父 View 中的滑动菜单可以检测到水平滑动

swift - 论问题结构的值(value)

android - android中的滑动展开动画

Javascript 切换点击关闭

swift - 是否可以在 Swift 中为结构或类中的成员使用可选值?

Swift - 未在表格 View 单元格中触发按钮操作

EMACS:扩展/扩展区域,使其包含整行