展开和折叠UITableView中的标题

我对iOS进行快速编程是个新手,但我正在尝试创建一个应用程序。我想有一个UITableView,其中有一些标题,如果单击它们,它们将根据打开还是关闭而打开或关闭。是否可以使用标头执行此操作,还是需要使用普通单元格?到目前为止,这就是我设置一切的方式。感谢您的任何帮助?

Her is an image of how it should look when it's open: A photo of how it should look open.

extension AboutVC: UITableViewDelegate, UITableViewDataSource{

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! SettingOptionCell
        cell.setting = settings[indexPath.section].options[indexPath.row]

        return cell
    }

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return settings[section].options.count
    }

     func numberOfSections(in tableView: UITableView) -> Int {
        return settings.count
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         let cell = tableView.dequeueReusableCell(withIdentifier: "settingCell") as! SettingsCell
        cell.setting = settings[section]
        cell.selectionStyle = .none
        return cell
    }
     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
         return 60
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    }

    }
}

class SettingOptionCell: UITableViewCell {
    var setting: String? {
        didSet {
            guard let string    = setting else {return}
            option.text         = string.capitalized
        }
    }
    let option: UILabel = {
        let lable           = UILabel()
        lable.text          = "test"
        lable.textColor     = UIColor.white
        lable.font          = UIFont(name: "AmericanTypewriter", size: 17)
        lable.translatesAutoresizingMaskIntoConstraints = false
        return lable
        }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addSubview(option)
        option.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 20).isActive = true
        option.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.backgroundColor = .none
    }



    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class SettingsCell: UITableViewCell {
    var setting: settingOptions? {
        didSet {
            guard let setting = setting else {return}
            self.title.text = setting.setting.uppercased()
        }
    }
    var title: UILabel = {
        let lable       = UILabel()
        lable.text      = "Test"
        lable.font      = UIFont(name: "AmericanTypewriter", size: 20)
        lable.textColor = .white
        lable.translatesAutoresizingMaskIntoConstraints = false
        return lable
    }()
    var container: UIView = {
        let view                                        = UIView()
        view.clipsToBounds                              = true
        view.backgroundColor                            = .none
        view.translatesAutoresizingMaskIntoConstraints  = false
        return view
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor                = .none
        self.isUserInteractionEnabled       = true
        contentView.addSubview(container)
        container.addSubview(title)
        title.leftAnchor.constraint(equalTo: container.leftAnchor).isActive                     = true
        title.centerYAnchor.constraint(equalTo: container.topAnchor,constant: 30).isActive      = true

        container.topAnchor.constraint(equalTo:contentView.topAnchor).isActive                  = true
        container.leftAnchor.constraint(equalTo:contentView.leftAnchor).isActive                = true
        container.rightAnchor.constraint(equalTo:contentView.rightAnchor).isActive              = true
        container.bottomAnchor.constraint(equalTo:contentView.bottomAnchor).isActive            = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
评论
  • qquas
    qquas 回复

    定义变量以存储表视图节状态

    var isCollapsed:Bool = true
    

    然后,您可以将UITapGestureRecognizer添加到表格视图标题视图中

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let cell = tableView.dequeueReusableCell(withIdentifier: "settingCell") as! SettingsCell
            cell.setting = settings[section]
            cell.selectionStyle = .none
            cell.isUserInteractionEnabled = true
            cell.tag = section
            cell.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didMenuItemClicked(_:))))
            return cell
    }
    

    下一步,您要配置表格视图单元以根据isCollapsed状态运行

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if !isCollapsed {
                let cell = tableView.dequeueReusableCell(withIdentifier: "header") as! SettingOptionCell
                cell.setting = settings[indexPath.section].options[indexPath.row]
                return cell
            }else{
                let cell = UITableViewCell()
                cell.translatesAutoresizingMaskIntoConstraints = false
                cell.heightAnchor.constraint(equalToConstant: 0).isActive = true
                cell.widthAnchor.constraint(equalToConstant: 0).isActive = true
                return cell
            }
    
    }
    

    现在,您可以编写函数来显示,隐藏子项,

    @objc func didMenuItemClicked(_ sender : UITapGestureRecognizer){
    
            isCollapsed = !isCollapsed
            myTable.reloadSections([sender.view!.tag], with: .fade)
    }