title: Collapsible input form with UITableView Swift 3
body: <p>I need to make a collapsible tableView for an input form, I have searched a lot and for two days I'm trying different codes, but they don't fit my need and I don't know how to change them to what I want. I've found a couple of examples but they are just for showing an array of objects. what I need is a form like this picture, but not populating the cells with an array of Strings (Like so many tutorials that I found) but rather making the exact same thing as this screenshot but collapsible so that when for instance when I tap on <code>Personal info</code> it collapses.</p>
  
  <p>This is one of the codes that I found online, but as I said I don't want to populate it with Array. I simply want to add as much Sections as I want with different cells and simply make them collapsible.</p>
  
  <p>(and btw with this code my sections show collapsed but when I tap on each the app crashes)</p>
  
  <blockquote>
    <p>TestVC.swift</p>
  </blockquote>
  
  <pre><code>class TestVC: UITableViewController, ExpandableHeaderViewDelegate {
  
  var sections = [
      Section(genre: "Animation", expanded: false),
      Section(genre: "Horror", expanded: false)
  ]
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  }
  
  
  // MARK: - Table view data source
  
  override func numberOfSections(in tableView: UITableView) -&gt; Int {
      // #warning Incomplete implementation, return the number of sections
      return sections.count
  }
  
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      // #warning Incomplete implementation, return the number of rows
      return 4
  }
  
  override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -&gt; CGFloat {
      return 44
  }
  
  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      if (sections[indexPath.section].expanded){
          return 44
      } else{
          return 0
      }
  }
  
  
  // Just to make it look nice
  override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -&gt; CGFloat {
      return 2
  }
  
  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -&gt; UIView? {
      let header = ExpandableHeaderView()
      header.customInit(title: sections[section].genre, section: section, delegate: self)
      return header
  }
  
  func toggleSection(header: ExpandableHeaderView, section: Int) {
      sections[section].expanded = !sections[section].expanded
  
      tableView.beginUpdates()
      for i in 0..&lt;6 {
          tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
      }
      tableView.endUpdates()
  }
  
  
  }
  </code></pre>
  
  <blockquote>
    <p>ExpandableHeaderView.swift</p>
  </blockquote>
  
  <pre><code>protocol ExpandableHeaderViewDelegate {
      func toggleSection(header: ExpandableHeaderView, section: Int)
  }
  
  class ExpandableHeaderView: UITableViewHeaderFooterView {
  
  var delegate: ExpandableHeaderViewDelegate?
  var section: Int!
  
  override init(reuseIdentifier: String?){
      super.init(reuseIdentifier: reuseIdentifier)
      self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(selectHeaderAction)))
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  func selectHeaderAction(gestureRecognizer: UITapGestureRecognizer){
      let cell = gestureRecognizer.view as! ExpandableHeaderView
      delegate?.toggleSection(header: self, section: cell.section)
  }
  
  func customInit(title: String, section: Int, delegate: ExpandableHeaderViewDelegate){
      self.textLabel?.text = title
      self.section = section
      self.delegate = delegate
  }
  
  override func layoutSubviews() {
      super.layoutSubviews()
      self.textLabel?.textColor = UIColor.white
      self.contentView.backgroundColor = UIColor.darkGray
  }
  
  
  
   }
  </code></pre>
  
  <p>(If there is other way than doing it with TableView that is also ok)</p>
  
  <p>Thank you in advance for any help.</p>
  
  <p><a href="https://i.stack.imgur.com/fSwiL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fSwiL.png" alt="this"></a></p>