Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, DataSendingDelegateProtocol, FieldsDelegateProtocol {
  4. var shouldCellBeExpanded:Bool = false
  5. var indexOfExpendedCell:NSInteger = -1
  6. // let kHeaderSectionTag: Int = 6900;
  7.  
  8. @IBOutlet weak var tableView: UITableView!
  9. var sectionArray = [String]()
  10. var sectionItemsArray = [String]()
  11.  
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14.  
  15. self.tableView!.tableFooterView = UIView()
  16. }
  17.  
  18. override func viewWillAppear(_ animated: Bool)
  19. {
  20. super.viewWillAppear(animated)
  21. }
  22.  
  23. override func didReceiveMemoryWarning() {
  24. super.didReceiveMemoryWarning()
  25. // Dispose of any resources that can be recreated.
  26. }
  27.  
  28. func sendDataToTableRow(myData: String) {
  29. self.sectionItemsArray.append(myData)
  30. tableView.reloadData()
  31. // print(iteamsArray)
  32. }
  33.  
  34. func sendDataToSectionLabel(myData: String) {
  35. self.sectionArray.append(myData)
  36. tableView.reloadData()
  37. print(sectionArray)
  38. }
  39.  
  40. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  41. if (segue.identifier == "segue") {
  42. let vc = segue.destination as! PopViewController
  43. vc.delegate = self
  44. }
  45. else if (segue.identifier == "fieldsSegue") {
  46. let vc = segue.destination as! FieldsViewController
  47. vc.delegate = self
  48. }
  49. }
  50. @objc func expandButnClicked(sender:UIButton){
  51.  
  52. print("expand button tapped")
  53.  
  54. shouldCellBeExpanded = !shouldCellBeExpanded
  55. indexOfExpendedCell = sender.tag
  56.  
  57. if shouldCellBeExpanded {
  58.  
  59. self.tableView.beginUpdates()
  60. self.tableView.endUpdates()
  61. }
  62. else {
  63. self.tableView.beginUpdates()
  64. self.tableView.endUpdates()
  65. }
  66. }
  67.  
  68. @IBAction func addBtn(_ sender: Any) {
  69. }
  70.  
  71. // MARK: - Tableview Methods
  72. func numberOfSections(in tableView: UITableView) -> Int {
  73.  
  74. return sectionArray.count
  75. }
  76.  
  77. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  78.  
  79. if (self.indexOfExpendedCell == section) {
  80. let arrayOfItems = self.sectionItemsArray[section]
  81. return arrayOfItems.count
  82. } else {
  83. return 0
  84. }
  85. }
  86.  
  87. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  88. if (self.sectionArray.count != 0) {
  89. return self.sectionArray[section] as? String
  90. }
  91. return ""
  92. }
  93.  
  94. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  95. return 44.0;
  96. }
  97.  
  98. /*func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat{
  99. return 0;
  100. }*/
  101.  
  102. func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
  103.  
  104. let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
  105. header.contentView.backgroundColor = UIColor.colorWithHexString(hexStr: "#0075d4")
  106. header.textLabel?.textColor = UIColor.white
  107.  
  108. let button = UIButton(frame: CGRect(x: 380, y: 10, width: 15, height: 15))
  109. button.backgroundColor = UIColor.green
  110. button.tag = section
  111. button.addTarget(self, action: #selector(expandButnClicked), for: .touchUpInside)
  112.  
  113. header.addSubview(button)
  114. }
  115.  
  116. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  117.  
  118. if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell {
  119. return 200
  120. }
  121. else {
  122. return 0
  123. }
  124. }
  125.  
  126. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  127.  
  128. let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableViewCell
  129.  
  130. cell.textLab?.textColor = UIColor.black
  131. cell.textLab.text = sectionItemsArray[indexPath.row]
  132. cell.backgroundColor = UIColor.black
  133. return cell
  134. }
  135.  
  136. func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
  137. tableView.deselectRow(at: indexPath, animated: true)
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement