Vikram Pote
Vikram Pote

Reputation: 5579

How to hide particular section in UITableViewController on change of UISwitch in swift

My UITableView have 3 sections 1)Show UISwitch to the user to ask whether you want to show image or not 2)Show two text fields 3)Show ImageView to display image

and i want to hide 3rd section on change of UISwitch. both 3 sections are static. and the action of uiswitch is given below

 @IBAction func stateChanged(switchState: UISwitch, tableView: UITableView )
{
    if switchState.on {
        showImageLB.text = "Yes"
        println( "The Switch is On")
    } else {
        showImageLB.text = "No"
        println("The Switch is Off")
    }
}

Please help me to get the solution. Thanks..

Upvotes: 3

Views: 886

Answers (1)

Shamas S
Shamas S

Reputation: 7549

Each time the UISwitch is flipped, you need to add a new row to your section that has ImageView.

To add more rows and to hide a section, all you need to do is to call reloadData on your tableView. Once you call this reloadData, 2 of your following functions will be called again.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath

All you have to do now, is that once your switch if flipped, save a boolean and return different values in your functions.

For example.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(switchState.on)
{
 return 2; // now you are hiding your last section. 
}
else
{
return 3; // now you are showing all 3 sections when switch is off.
}
}

Upvotes: 2

Related Questions