mybecks
mybecks

Reputation: 2473

Remove or Add UITableViewCell according to state of UISwitch

I have a table with four rows. Two of them should be displayed initially -- rows 1 and 2. The other two should only appear if the state of a UISwitch, which is located in row 2, is "On". If the state is set to "Off", then they should disappear.

I have created a UITableViewCell with holds the UISwitch:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSMutableArray *dict = [dictionary objectForKey:@"DATA"];

    //the location of the switch!

        if(indexPath.row == 1){

            SwitchCell *cell = (SwitchCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
            if (cell == nil) {

                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SwitchCell" owner:self options:nil];
                cell = (SwitchCell *)[nib objectAtIndex:0];
            }

            cell.title1.text = [dict objectAtIndex:indexPath.row];

            [cell.switchSpecifyEnd addTarget:self action:@selector(switchSpecifyEnd) forControlEvents:UIControlEventValueChanged];
            mySwitch = cell.switchSpecifyEnd;

                return cell;
        }else{
        static NSString *CellIdentifier1 = @"CustonCell";

        DateTimeCell *cell = (DateTimeCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
        if (cell == nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DateTimeCell" owner:self options:nil];
            cell = (DateTimeCell *)[nib objectAtIndex:0];
        }

        cell.description.text = [dict objectAtIndex:indexPath.row];
        cell.dateTime.text = self.date;

        return cell;
    }
}



-(void) switchSpecifyEnd{
    //displays only 0 (defined initial state of  my switch)
    NSLog(@"SWITCH - Specify End: %d",  mySwitch.state);


    //    [tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //    [tableView1 reloadData];
}

How can I get rows 3 and 4 to not be displayed initially, and let them appear if the switch's state is changed to "On"?

Upvotes: 0

Views: 499

Answers (2)

Dorian Roy
Dorian Roy

Reputation: 3104

You need to set a variable in your class that indicates if the switch is on or of. Use that variable in tableView:numberOfRowsInSection to determine if you need to return 2 or 4 as the number of cells.

After you flipped the switch to "On", you need to change that variable and call reloadData (you already have this call in your switch action but it is commented out).

The table view controller will then call tableView:numberOfRowsInSection again and get the new number of cells. Then tableView:cellForRowAtIndexPath: will be called again and ask for all the cells again, now including number 3 and 4.

Upvotes: 1

RDM
RDM

Reputation: 5066

I think the best practice for this would be to:

  • Move the UISwitch out of the tableView, just put it above the table
  • Use an NSMutableArray as data source

Just add or remove the two elements to or from the data source array and call [tableView reloadData] after switching the switch.

This implies that, instead of just making the tableViewCell invisible, you rather remove the entire data behind it every time you want to hide it. This can be less ideal, so you could also opt for an NSArray for holding your data. Instead of then removing/adding the data from/to it, you should have a global BOOL variable to keep track of whether or not the 2 cells should be shown. In your UITableViewDataSource methods, wrap the contents of the functions in an if clause. For example for - (NSInteger)tableView:numberOfRowsInSection:

// assume we have a global BOOL showAllRows which is manipulated with the UISwitch
// assume the data for the UITableView is loaded from an NSArray * data
if (self.showAllRows)
    return self.data.count;
else
    return self.data.count - 2; 

Then you should do the same in the - (UITableViewCell*)tableView:cellForRowAtIndexPath: method to configure your cells.

Hope this helps!

Upvotes: 1

Related Questions