Learn Courses My Dashboard

Tableviewcell expanding by tapping header cell

I trying to expand the tableviewcell by tapping the header cell. But I want to know how to pass the collapse bool with sections in header delegate function. When I tap on header the delegate function is calling correctly.

Please correct.

class ViewController: UIViewController, UIGestureRecognizerDelegate {

//MARK: - IBOutlets
@IBOutlet weak var bottomView: UIView!
@IBOutlet weak var subcriptionTableview: UITableView!

var reloadSections: ((_ section: Int) -> Void)?
var isCollapsible = false
var isCollapsed = false

let imgArray = ["basic","standard","advanced","premium"]
let amtArray = ["$10 / month","$25 / month","$30 / month","$40 / month"]
let planArray = ["Basic","Standard","Advanced","Premium"]
let expandViewArray = ["Lorem ipsum is a dummy text content in a paragraph.","Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium.","At vero eos et accusamus et iusto odio dignissimos."]

//MARK: - LifeCycle
override func viewDidLoad() {
    reloadSections = { [weak self] (section: Int) in
        self?.subcriptionTableview?.reloadSections([section], with: .automatic)
    subcriptionTableview.layer.cornerRadius = 10
    subcriptionTableview.register(UINib(nibName: "SubsPlanHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: "SubsPlanHeader")
    subcriptionTableview.register(UINib(nibName: "ExpandTableViewCell", bundle: nil), forCellReuseIdentifier: "expandcell")
    bottomView.clipsToBounds = true
    bottomView.layer.cornerRadius = 15
    bottomView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]


extension ViewController : UITableViewDelegate,UITableViewDataSource,HeaderViewDelegate {

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard isCollapsible else{
        return expandViewArray.count
    if isCollapsed == true {
        return 0
        return expandViewArray.count

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = subcriptionTableview.dequeueReusableCell(withIdentifier: "expandcell", for: indexPath) as! ExpandTableViewCell
    cell.labelPlanDetail.text = expandViewArray[indexPath.row]
    return cell

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerCell = subcriptionTableview.dequeueReusableHeaderFooterView(withIdentifier: "SubsPlanHeader") as! SubsPlanHeader
    headerCell.labelAmt.text = amtArray[section]
    headerCell.imgView.image = UIImage(named: imgArray[section])
    headerCell.labelPlanName.text = planArray[section]
    headerCell.labelLine.isHidden = true
    headerCell.delegate = self
    headerCell.section = section
    return headerCell

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableView.automaticDimension

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 150

func toggleSection(header: SubsPlanHeader, section: Int) {
    if isCollapsible {
        // Toggle collapse
        let collapsed = !isCollapsed
        isCollapsed = collapsed
        //  header.setCollapsed(collapsed: collapsed)
        // Adjust the number of the rows inside the section


UITableViewHeaderFooterView :

protocol HeaderViewDelegate: AnyObject { func toggleSection(header: SubsPlanHeader, section: Int) }

class SubsPlanHeader: UITableViewHeaderFooterView {

// MARK: - IBOutlets
@IBOutlet weak var subView: UIView!
@IBOutlet weak var labelAmt: UILabel!
@IBOutlet weak var imageArrow: UIImageView!
@IBOutlet weak var imageCheck: UIImageView!
@IBOutlet weak var labelPlanName: UILabel!
@IBOutlet weak var labelLine: UILabel!
@IBOutlet weak var imgView: UIImageView!

var section: Int?
weak var delegate: HeaderViewDelegate?

override func awakeFromNib() {
    addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapHeader)))


@objc private func didTapHeader() {
    if let _section = section{
    delegate?.toggleSection(header: self, section: _section)


Thanks in Advance.