0

I have a UITableView that I am using to display settings for my application. What I want to achieve is animation of two UITableViewCells when a UISwitch's value is changed.

A couple of nice examples are the Wi-Fi settings in iOS's Settings. When you turn the Wi-Fi off, the sections below that cell fade out and upward until they are gone. Inversely, they fade in when the Wi-Fi is enabled. Also, Tweetbot 4 does this beautifully in the Display settings when choosing a setting under Theme. When enabled, two cells move upward until gone and then one cell moves downward to take the place of the other two cells.

I was able to find a tutorial with gifs for achieving the hiding and display of cells, however, it is without a nice animation. Instead, the cells just disappear and reappear abruptly.

My tableView:heightForRowAtIndexPath: method looks like this:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.row == 1 && authenticationSettingsSwitch.on == false {
        return 0
    }
    if indexPath.row == 2 && authenticationSettingsSwitch.on == false {
            return 0
    }
    return 44
}

If the switch is on, the two cells are visible, with a height of 44. If the switch is off, the two cells are collapsed with a height of 0.

The aforementioned takes place in my UISwitch's action method:

@IBAction func authenticationSettingsSwitchFlipped(sender: UISwitch) {
    let requireAuthentication = sender.on == true ? true : false
    defaults.setBool(requireAuthentication, forKey: Settings.Authentication.rawValue)
    defaults.synchronize()
    if requireAuthentication {
        delay(0.3) { () -> Void in
            let authenticationNavigationController = self.storyboard?.instantiateViewControllerWithIdentifier("authenticationNavigationController") as! UINavigationController
            let authenticationViewController = authenticationNavigationController.topViewController as! AuthenticationViewController
            authenticationViewController.settingsDelegate = self
            self.presentViewController(authenticationNavigationController, animated: true, completion: nil)
        }
    } else {
        tableView.reloadData()
    }
}

I have also looked at tableView:insertRowsAtIndexPaths:withRowAnimation, however, I keep getting crashes for trying to call this method without using a data source since the cells are static.

Below are a couple of screenshots that may help you see more clearly what I am wanting:

enter image description here

As stated above, I can get the cells to appear and disappear via a UISwitch in the first cell (top set of screenshots), but they do so abruptly. In the bottom set of screenshots (Tweetbot 4), the animation is gorgeous and smooth.

Can anyone point me in the direction of how to achieve what I am after?

Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49
  • No idea how to do this with static cells. Thanks for the challenge though. I did take a few minutes and crush out an example using dynamic cells. Just to prove that I could do it. Not sure if it will help you. https://github.com/regnerjr/HideAndShowTableViewCellsBasedOnSwitch – JMFR Oct 07 '15 at 05:28

1 Answers1

0

I found a solution to enable a nice animation for showing/hiding static UITableViewCells here.

Community
  • 1
  • 1
Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49