Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import M13Checkbox
- protocol HeaderViewDelegate {
- func toggleHeader(header : HeaderView, section : Int)
- }
- protocol CustomHeaderDelegate: class {
- func didTapButton(in section: Int, headerView : HeaderView, button : M13Checkbox)
- }
- class HeaderView: UITableViewHeaderFooterView {
- @IBOutlet weak var stateCheckBox: M13Checkbox!
- @IBOutlet weak var stateNameLabel: UILabel!
- @IBOutlet weak var favouriteState: M13Checkbox!
- var delegate : HeaderViewDelegate?
- weak var delegateHeader: CustomHeaderDelegate?
- var sectionNumber : Int!
- var section : Int!
- override func awakeFromNib() {
- stateCheckBox.boxType = .square
- stateCheckBox = .bounce(.fill)
- favouriteState.boxType = .circle
- favouriteState.setMarkType(markType: .radio, animated: true)
- favouriteState.stateChangeAnimation = .bounce(.stroke)
- }
- override init(reuseIdentifier: String?) {
- super.init(reuseIdentifier: reuseIdentifier)
- self.addGestureRecognizer(UITapGestureRecognizer(target : self, action: #selector(selectHeaderView)))
- }
- required init?(coder aDecoder: NSCoder) {
- super.init(coder : aDecoder)
- self.addGestureRecognizer(UITapGestureRecognizer(target : self, action: #selector(selectHeaderView)))
- }
- func selectHeaderView(gesture : UITapGestureRecognizer) {
- let cell = gesture.view as! HeaderView
- delegate?.toggleHeader(header: self, section: cell.section)
- }
- func customInit(titleLabel : String, section : Int, delegate : HeaderViewDelegate) {
- self.stateNameLabel.text = titleLabel
- self.section = section
- self.delegate = delegate
- }
- @IBAction func selectPrimaryCondition(_ sender: M13Checkbox) {
- // get section when favourite state radioButton is selected
- delegateHeader?.didTapButton(in: sectionNumber, headerView : self, button : sender)
- }
- override func prepareForReuse() {
- // What do do here…??
- }
- }
- func numberOfSections(in tableView: UITableView) -> Int {
- return states.count
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return states[section].cities.count
- }
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 50.0
- }
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if (states[indexPath.section].expanded) {
- return 44
- }else{
- return 0.0
- }
- }
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerviewcell") as! HeaderView
- var list = states[section]
- headerCell.customInit(titleLabel: list.stateName, section: section, delegate: self)
- return headerCell
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "subcells") as! CollapsibleCell
- cell.selectionStyle = .none
- cell.textLabel?.text = states[indexPath.section].cities[indexPath.row]
- return cell
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- // works for headers or cell??
- }
- func toggleHeader(header : HeaderView, section : Int){
- states[section].expanded = !states[section].expanded
- tableView.beginUpdates()
- for i in 0 ..< states[section].cites.count {
- tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
- }
- tableView.endUpdates()
- }
- extension ViewController: HeaderDelegate {
- func didTapButton(in section: Int, headerView : HeaderView, button : M13Checkbox) {
- print("(section)")
- }
- }
Add Comment
Please, Sign In to add comment