Guest User

Untitled

a guest
Mar 21st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. import UIKit
  2. import M13Checkbox
  3.  
  4. protocol HeaderViewDelegate {
  5. func toggleHeader(header : HeaderView, section : Int)
  6. }
  7.  
  8. protocol CustomHeaderDelegate: class {
  9. func didTapButton(in section: Int, headerView : HeaderView, button : M13Checkbox)
  10. }
  11.  
  12. class HeaderView: UITableViewHeaderFooterView {
  13.  
  14. @IBOutlet weak var stateCheckBox: M13Checkbox!
  15. @IBOutlet weak var stateNameLabel: UILabel!
  16. @IBOutlet weak var favouriteState: M13Checkbox!
  17.  
  18. var delegate : HeaderViewDelegate?
  19. weak var delegateHeader: CustomHeaderDelegate?
  20. var sectionNumber : Int!
  21. var section : Int!
  22.  
  23. override func awakeFromNib() {
  24. stateCheckBox.boxType = .square
  25. stateCheckBox = .bounce(.fill)
  26.  
  27. favouriteState.boxType = .circle
  28. favouriteState.setMarkType(markType: .radio, animated: true)
  29. favouriteState.stateChangeAnimation = .bounce(.stroke)
  30. }
  31.  
  32. override init(reuseIdentifier: String?) {
  33. super.init(reuseIdentifier: reuseIdentifier)
  34. self.addGestureRecognizer(UITapGestureRecognizer(target : self, action: #selector(selectHeaderView)))
  35. }
  36.  
  37. required init?(coder aDecoder: NSCoder) {
  38. super.init(coder : aDecoder)
  39. self.addGestureRecognizer(UITapGestureRecognizer(target : self, action: #selector(selectHeaderView)))
  40. }
  41.  
  42. func selectHeaderView(gesture : UITapGestureRecognizer) {
  43. let cell = gesture.view as! HeaderView
  44. delegate?.toggleHeader(header: self, section: cell.section)
  45. }
  46.  
  47. func customInit(titleLabel : String, section : Int, delegate : HeaderViewDelegate) {
  48. self.stateNameLabel.text = titleLabel
  49. self.section = section
  50. self.delegate = delegate
  51. }
  52.  
  53. @IBAction func selectPrimaryCondition(_ sender: M13Checkbox) {
  54. // get section when favourite state radioButton is selected
  55. delegateHeader?.didTapButton(in: sectionNumber, headerView : self, button : sender)
  56. }
  57.  
  58. override func prepareForReuse() {
  59. // What do do here…??
  60. }
  61. }
  62.  
  63. func numberOfSections(in tableView: UITableView) -> Int {
  64. return states.count
  65. }
  66.  
  67. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  68. return states[section].cities.count
  69. }
  70.  
  71. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  72. return 50.0
  73. }
  74.  
  75. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  76. if (states[indexPath.section].expanded) {
  77. return 44
  78. }else{
  79. return 0.0
  80. }
  81. }
  82.  
  83. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  84. let headerCell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerviewcell") as! HeaderView
  85. var list = states[section]
  86. headerCell.customInit(titleLabel: list.stateName, section: section, delegate: self)
  87. return headerCell
  88. }
  89.  
  90. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  91. let cell = tableView.dequeueReusableCell(withIdentifier: "subcells") as! CollapsibleCell
  92. cell.selectionStyle = .none
  93. cell.textLabel?.text = states[indexPath.section].cities[indexPath.row]
  94. return cell
  95. }
  96.  
  97. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  98. // works for headers or cell??
  99. }
  100.  
  101. func toggleHeader(header : HeaderView, section : Int){
  102. states[section].expanded = !states[section].expanded
  103.  
  104. tableView.beginUpdates()
  105. for i in 0 ..< states[section].cites.count {
  106. tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
  107. }
  108. tableView.endUpdates()
  109. }
  110.  
  111. extension ViewController: HeaderDelegate {
  112. func didTapButton(in section: Int, headerView : HeaderView, button : M13Checkbox) {
  113. print("(section)")
  114. }
  115. }
Add Comment
Please, Sign In to add comment