Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6853649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:32:14+00:00 2026-05-27T01:32:14+00:00

I have a table with four rows. Two of them should be displayed initially

  • 0

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”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:32:15+00:00Added an answer on May 27, 2026 at 1:32 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with two rows with four columns. I have text in
In my index.php I have TABLE with four rows. One of them contains INPUT
I have four rows in my table. Only three are shown. $query = SELECT
I am wanting to have a view table that aggregates rows from four tables.
I have a table that holds only two columns - a ListID and PersonID.
I have a table with four columns: id, from, to, msg. The rows can
I have a table of rows holding four different fields of numbers: people, debris,
I have two SQL Server tables. One table has four nvarchar(50) nullable columns and
I have a table with four columns. I'm adding rows to the table dynamically
I have a table with four columns: PartNumber, ValvePartNumber, ActuatorPartNumber, Price I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.