Search code examples
iosswiftuitableviewuisegmentedcontrol

Hide or Show Static TableViewController Cells with a Segmented Control


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.

Code:

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> 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)

}

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?

UPDATE:

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?

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

    }

}

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.

@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) -> 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)
}

Solution

  • The solution to my final update is to create var selectedSegment = 0 at the top of the class.

    Then add:

    @objc func segChange(sender: UISegmentedControl) {
    
        self.selectedSegment = sender.selectedSegmentIndex //Add this line of code
    
       tableView.reloadData()
    
    }
    

    This tells the segmented control to update its selected index. This was causing mine to not respond when I changed the index.