Expand/collapse section in UITableView in iOS

前端 未结 17 1048
野的像风
野的像风 2020-11-22 08:49

Could somebody tell me the way to perform UITableView expandable/collapsible animations in sections of UITableView as below?

<

17条回答
  •  猫巷女王i
    2020-11-22 09:44

    I am adding this solution for completeness and showing how to work with section headers.

    import UIKit
    
    class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet var tableView: UITableView!
        var headerButtons: [UIButton]!
        var sections = [true, true, true]
    
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.dataSource = self
            tableView.delegate = self
    
            let section0Button = UIButton(type: .detailDisclosure)
            section0Button.setTitle("Section 0", for: .normal)
            section0Button.addTarget(self, action: #selector(section0Tapped), for: .touchUpInside)
    
            let section1Button = UIButton(type: .detailDisclosure)
            section1Button.setTitle("Section 1", for: .normal)
            section1Button.addTarget(self, action: #selector(section1Tapped), for: .touchUpInside)
    
            let section2Button = UIButton(type: .detailDisclosure)
            section2Button.setTitle("Section 2", for: .normal)
            section2Button.addTarget(self, action: #selector(section2Tapped), for: .touchUpInside)
    
            headerButtons = [UIButton]()
            headerButtons.append(section0Button)
            headerButtons.append(section1Button)
            headerButtons.append(section2Button)
        }
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return sections.count
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return sections[section] ? 3 : 0
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cellReuseId = "cellReuseId"
            let cell = UITableViewCell(style: .default, reuseIdentifier: cellReuseId)
            cell.textLabel?.text = "\(indexPath.section): \(indexPath.row)"
            return cell
        }
    
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            return headerButtons[section]
        }
    
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 44
        }
    
        @objc func section0Tapped() {
            sections[0] = !sections[0]
            tableView.reloadSections([0], with: .fade)
        }
    
        @objc func section1Tapped() {
            sections[1] = !sections[1]
            tableView.reloadSections([1], with: .fade)
        }
    
        @objc func section2Tapped() {
            sections[2] = !sections[2]
            tableView.reloadSections([2], with: .fade)
        }
    
    }
    

    Link to gist: https://gist.github.com/pawelkijowskizimperium/fe1e8511a7932a0d40486a2669316d2c

提交回复
热议问题