Sivani Sangameswaran
Sivani Sangameswaran

Reputation: 31

HeaderView tapping tableviewcell expand

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() {
    super.viewDidLoad()
    
    reloadSections = { [weak self] (section: Int) in
        self?.subcriptionTableview?.beginUpdates()
        self?.subcriptionTableview?.reloadSections([section], with: .automatic)
        self?.subcriptionTableview?.endUpdates()
    }
    
    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
    }else{
        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
        reloadSections?(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() {
    super.awakeFromNib()
    addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(didTapHeader)))

}

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

}

Thanks in Advance.

Upvotes: 0

Views: 394

Answers (1)

matsotaa
matsotaa

Reputation: 133

It seems to me that you can't reached delegate method execution as you want in reason that you have got property isCollapsible always false since viewController initiated. And you are toggling it only when it is true

Any way instead of

let collapsed = !isCollapsed 
isCollapsed = collapsed

you can use

isCollapsed = !isCollapsed

since by tap on the header you always need to reload section for hide/unhide try to remove if condition and reconsider some code

just saw these strings:

var isCollapsible = false
var isCollapsed = false

my advise - try to refactor for simplifying. I don't know your goals, but it seems odd by first look

Upvotes: 0

Related Questions