I am working on Collapsable-Expandable Tableview cell where I have created separate class and xib for UITableViewHeaderFooterView and linked both to each Other. Xib view contains two labels one for Title and one for + or - to indicate section is collapsed or expanded. Now in my One of the view controller I have all the necessary methods of tableview written and below is the code for the same.
var sections = [
Section(Title: "Sec 1", Indicator : "-" , expanded : true, moview : ["Simba1","TaleSpin1"]),
Section(Title: "Sec 2", Indicator : "+" ,expanded : false, moview : ["Simba2","TaleSpin2"]),
Section(Title: "Sec 3", Indicator : "+" ,expanded : false, moview : ["Simba3","TaleSpin3"]),
Section(Title: "Sec 4", Indicator : "+" ,expanded : false, moview : ["Simba4","TaleSpin4"])
]
var selectIndexPath: IndexPath!
override func viewDidLoad() {
super.viewDidLoad();
selectIndexPath = IndexPath(row: -1, section: -1)
tableView.register(UINib(nibName: "HeaderView", bundle: nil), forHeaderFooterViewReuseIdentifier: "HeaderView");
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].movie.count
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if sections[indexPath.section].expanded {
return 44
} else {
return 0
}
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "ExpandableHeader") as! ExpandableHeaderView;
let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "HeaderView") as! HeaderView
headerView.customInit(title: sections[section].Title , indicator: sections[section].Indicator , section: section, delegate: self)
return headerView
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell")!;
cell.textLabel?.text = sections[indexPath.section].movie[indexPath.row];
return cell
}
func toggleSection(header: HeaderView, section: Int) {
print("expanded: \(!sections[section].expanded)");
sections[section].expanded = !sections[section].expanded
sections[section].Indicator = "+"
tableView.beginUpdates()
for i in 0 ..< sections[section].movie.count {
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}
I want to change the label + or - based on whether section is collapsed or expanded on user tap. I tried adding sections[section].Indicator = "+"
this line in toggleSection
function but of no use.
Any help will be appreciated.