'Make different collapsable sections in tableView swift iOS

I want to implement the different expand/collapse section in tableView and each sections contain the different kind of data.I'm getting the index of section when user click on button. Now i want when user click on button section collapse and expand.Initially first time all sections data should be hidden when user first time comes. See my code and screenshot.

ViewController Code:

enum ProfileItems: Int{
    case namePicture = 0
    case about = 1
}

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var items:[ProfileItems] = [.namePicture, .about]
    var profile:[Profiles] = [
        Profiles(name: "Ki", profileImage: "slide2"),
        Profiles(name: "Kaleem", profileImage: "slide3"),
        Profiles(name: "Jameel", profileImage: "slide2")
    ]
    var about: [About] = [
        About(title: "Johns"),
        About(title: "Dons")
    ]
    var isHiddenSection: Bool = false {
        didSet {
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        registerCell()
    }
    
    func registerCell() {
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: String(describing: PictureTvCell.self), bundle: .main), forCellReuseIdentifier: String(describing: PictureTvCell.self))
        tableView.register(UINib(nibName: String(describing: AboutTVCell.self), bundle: .main), forCellReuseIdentifier: String(describing: AboutTVCell.self))
        tableView.register(UINib(nibName: String(describing: HeaderTVCell.self), bundle: .main), forCellReuseIdentifier: String(describing: HeaderTVCell.self))
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: HeaderTVCell.self)) as? HeaderTVCell else {return UIView()}
        switch items[section]{
        case .namePicture:
            cell.lableName.text = "Info"
        case .about:
            cell.lableName.text = "Collection"
        }
        cell.buttonCollpase.tag = items[section].rawValue
        cell.delegate = self
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch items[section] {
        case .namePicture:
            return profile.count
        case .about:
            return about.count
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch items[indexPath.section] {
        case .namePicture:
            return configurePictureCell(tableView, cellForRowAt: indexPath)
        case .about:
            return configureAboutCell(tableView, cellForRowAt: indexPath)
        }
    }

    func configurePictureCell(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: PictureTvCell.self)) as? PictureTvCell else {return UITableViewCell()}
        cell.profiles = self.profile[indexPath.row]
        return cell
    }
    
    func configureAboutCell(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: AboutTVCell.self)) as? AboutTVCell else {return UITableViewCell()}
        cell.about = self.about[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
}
extension ViewController: CollpaseSection {
    func getIndex(_ index: Int) {
        switch items[index] {
        case .namePicture:
            print("picture")
        case .about:
            print("about")
            tableView.reloadData()
        }
    }
}

TableViewHederCell Code:

protocol CollpaseSection:AnyObject {
    func getIndex(_ index: Int)
}
class HeaderTVCell: UITableViewCell {
    @IBOutlet weak var lableName: UILabel!
    @IBOutlet weak var buttonCollpase: UIButton!
    weak var delegate: CollpaseSection?

    @IBAction func didTapCollapse(_ sender: UIButton) {
        delegate?.getIndex(sender.tag)
    }
}

Screenshot current output: enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source