Hiding the table view cells with switch ON/OFF in iPhone

This may be a simple question for you experts but I can’t figure out the correct solution.

In my app I created a a table view to list a no of items.

  • Fetching JSON data after failed retrieval
  • Thread safe instantiation of a singleton
  • UIWebView is not deallocating memory
  • iOS TableView crash loading different data
  • Core Data Import - Not releasing memory
  • How to lose margin/padding in UITextView?
  • In the first row of the cell there a switch (ON/OFF).

    So my client need to hide the bottom cells(other than zeroth cell) when switch is OFF and show all the cells when switch is ON.

    I created the switch through code.

    Can anyone please help me with how to do this?

    Thanks in advance.

    3 Solutions Collect From Internet About “Hiding the table view cells with switch ON/OFF in iPhone”

    numberOfRows dataSource method – make it return 1 and reloadTableView

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      /// ....
                self.switchView = [[[UISwitch alloc] initWithFrame:CGRectZero]autorelease];
                cell.accessoryView = self.switchView;
                [self.switchView setOn:NO animated:NO];
                [self.switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    
    //....
    
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       if [self.switchView.on]
           return 1;
       else 
       {
          // normal code return
       }
    }
    
    - (void) switchChanged:(id)sender {
         [self.tableView reloadData];
    } 
    

    You could use insertRowsAtIndexPaths, this will not only add, but also do the animation of adding new row

    first add target UIControlEventTouchUpInside into UISwitch.

    [switch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];

    -(void) switchChange:(UISwitch*) sender{

           isSwitchOn = sender.on; //edited
        [myTable beginUpdates];
        NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
        NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
        //you may add as many row as you want here.
    
        [myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationMiddle];
        [myTable endUpdates];
    

    }

    and make sure you will add how many rows you will insert in numberOfRowsInSection:

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
      {

      if (isSwitchOn) return 1;

      else
      return 3; //how many rows would have after switched on

    }

    You might want to check [myTable insertSections:withRowAnimation: to insert another section and do the same as above. but remember to state how many section you will add, in numberOfSectionsInTableView:

    UPDATE :

    implement deleteRowsAtIndexPaths: if switch off.

    -(void) switchChange:(UISwitch*) sender{

        isSwitchOn = sender.on;
    
      if(isSwitchOn){
            [myTable beginUpdates];
             NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
             NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
             //you may add as many row as you want here.
    
            [myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil]  withRowAnimation:UITableViewRowAnimationMiddle];
            [myTable endUpdates];
        }
      else{
            [myTable beginUpdates];
             NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
             NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
             //you may add as many row as you want here.
    
            [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil]  withRowAnimation:UITableViewRowAnimationFade];
            [myTable endUpdates]; 
     }
    

    }

    There’s a UITableViewDataSource method called tableView:numberOfRows:inSections method.Just check for the state of the switch and return 1 if it’s OFF or all the rows if it is ON. Just call the [tableView reloadData] method each time you switch ON and OFF. As simple as that.