title: Hide or Show Static TableViewController Cells with a Segmented Control
body: <p>I am attempting to hide or show tableviewcontroller cells using a segmented control and a switch. The code below is not working and it will only print if I touch inside of the cell. I believe the touch causes the table to reload.</p>
  
  <p>Code:</p>
  
  <pre><code>override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      let cell = super.tableView(tableView, cellForRowAt: indexPath)
  
      switch(profileSegment.selectedSegmentIndex) {
  
      case 0:
          print("Seg Change")
          if cell == self.reviewsCell {
              return 0
          }
          tableView.reload()
          break
  
      case 1:
          print("Seg Change")
          if cell == self.watchCell {
              return 0
          }
          if cell == self.watchedCell {
              return 0
          }
          if cell == self.reviewsCell {
              return 500
          }
          tableView.reload()
          break
  
      default:
          break
  
      }
  
      return super.tableView(tableView, heightForRowAt: indexPath)
  
  }
  </code></pre>
  
  <p>Why is the tableview not reloading when the switch is made? Is there a more efficient way to achieve this other than changing the size to 0?</p>
  
  <p>UPDATE:</p>
  
  <p>I tried another route and made partial success. The switch works but only makes the cells appear blank. What would make the cells disappear completely?</p>
  
  <p>CODE:</p>
  
  <pre><code>func segChange(sender: UISegmentedControl) {
  
  switch(profileSegment.selectedSegmentIndex) {
  
      case 0:
          self.reviewCell.isHidden = true
          self.cell.isHidden = false
          tableView.reloadData()
  
          break
  
      case 1:
          self.reviewCell.isHidden = false
          self.cell.isHidden = true
          tableView.reloadData()
  
          break
  
      default:
          break
  
      }
  
  }
  </code></pre>
  
  <p>UPDATE 2: If I use the code below the tableView will change the cells the first time I select the new segment index but the segment control does not change and becomes unresponsive. Why is the segment responding this way? If I fix this the question will be solved.</p>
  
  <pre><code>@objc func segChange() {
  
     tableView.reloadData()
  
  }
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
  profileSegment.addTarget(self, action: #selector(segChange), for: .valueChanged)
  
  
  }
  
  
  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -&gt; CGFloat {
      let cell = super.tableView(tableView, cellForRowAt: indexPath)
  
      switch(profileSegment.selectedSegmentIndex) {
  
      case 0:
          if cell == self.reviewsCell {
              return 0
          }
          break
  
      case 1:
          if cell == self.watchCell {
              return 0
          }
          if cell == self.watchedCell {
              return 0
          }
          if cell == self.reviewsCell {
              return 500
          }
          break
  
      default:
          break
  
      }
  
      return super.tableView(tableView, heightForRowAt: indexPath)
  }
  </code></pre>