0

In the Apple reminders App, and in the details screen of a remind, when you switch on the control "Remind me at a location", a row "Location" is added (in fact a table view cell). I would like to do the same in one of my application, when a switch control is actived 2 cells are added... how can i do this? Thank you for your help

  • possible duplicate of [Expand/collapse section in UITableView](http://stackoverflow.com/questions/1938921/expand-collapse-section-in-uitableview) – Wain Jan 15 '14 at 14:10

3 Answers3

0

in the method handling the change of your UISwitch you could either just reload the complete tableview using -(void)reloadData or (much nicer) use - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

rist
  • 578
  • 2
  • 9
0

You can use UISwitch as accessory view in one of your cells. However, you can face problems with reusing cells. If you need only one row with switch, you can simply add the UISwitch as a strong property in your TableViewController. You have to create it when initializing the controller:

self.locationSwitch = [[UISwitch alloc] init];
    [self.locationSwitch addTarget:self action:@selector(handleSwitchValueChanged:) forControlEvents:UIControlEventValueChanged];

And then you can use it as an accessory view for your UITableViewCell:

cell.accessoryView = self.locationSwitch;

And you have to add (or remove) rows when switch value changes":

-(void)handleSwitchValueChanged:(id)sender
{
    NSIndexPath* indexPath = // calculate your index path;
    if(self.locationSwitch.on) {
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
  }
  else {    
    [self.tableView insertRowsAtIndexPaths:@[indexPath]   withRowAnimation:UITableViewRowAnimationAutomatic];
  }
}

And also remember to update your data source, so the value returned by tableView:numberOfRowsInSection is consistent with value of self.locationSwitch.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
0

Check out this Project

https://github.com/singhson/Expandable-Collapsable-TableView

It may helps you.

PunjabiCoder
  • 525
  • 2
  • 8