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 9011015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:37:01+00:00 2026-06-16T02:37:01+00:00

My tableView has a header with a segmented control in it. Segment 0 will

  • 0

My tableView has a header with a segmented control in it. Segment 0 will show my tableView with 3 rows and segment 1 will show it with 4 rows.

In each of those cells I’m adding a textField subview. So on segment zero I’ll have three rows with Zip Code, Radius and API Key. With segment 1 I’ll have four rows with Latitude, Longitude, Radius and API Key. However, when I toggle back and forth, the textField subviews aren’t being removed properly. (The Radius and API Key fields are the same field but they’ll show on different rows when toggled.)

I have tried tagging the textFields and then removing the textField with that tag but that was erratic. I’ve tested for the existence of a textField and if it existed, remove it on toggle. (this is in the example code below.)
I’ve also tried messing around with the segmentChanged method to no avail.

Please take a look at my code, I’d appreciate any pointers as I’m not sure where and how best to remove those subviews.

enter image description here enter image description here

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if ([mySegmentControl selectedSegmentIndex] == 0) {
            return 3;
        } else {
            return 4;
        }

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    if ([mySegmentControl selectedSegmentIndex] == 0) {

        if (latField) {
            [latField removeFromSuperview];
            [longField removeFromSuperview];
            [radiusField removeFromSuperview];
            [apiKeyField removeFromSuperview];

        }

        if ([indexPath row] == 0 && [indexPath section] == 0) {

            zipCodeField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];   
            [[cell textLabel] setText:@"Zip Code:"];
            [cell.contentView addSubview:zipCodeField];


        } else if ([indexPath row] == 1 && [indexPath section] == 0) {

            radiusField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];            
            [[cell textLabel] setText:@"Radius:"];
            [cell.contentView addSubview:radiusField];

        } else if ([indexPath row] ==2 && [indexPath section] == 0) {

            apiKeyField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 145, 30)];
            [[cell textLabel] setText:@"API Key:"];
            [cell.contentView addSubview:apiKeyField];

    }

    else if ([mySegmentControl selectedSegmentIndex] == 1) {

        if (zipCodeField) {
            [zipCodeField removeFromSuperview];
            [radiusField removeFromSuperview];
            [apiKeyField removeFromSuperview];
        }

        if ([indexPath row] == 0 && [indexPath section] == 0) {

            latField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
            [[cell textLabel] setText:@"Latitude:"];
            [cell.contentView addSubview:latField];

        } else if ([indexPath row] ==1 && [indexPath section] == 0) {

            longField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
            [[cell textLabel] setText:@"Longitude:"];
            [cell.contentView addSubview:longField];

        } else if ([indexPath row] ==2 && [indexPath section] == 0) {

            radiusField = [[UITextField alloc] initWithFrame:CGRectMake(135, 10, 145, 30)];
            [[cell textLabel] setText:@"Radius:"];
            [cell.contentView addSubview:radiusField];

        } else if ([indexPath row] == 3 && [indexPath section] == 0) {

            apiKeyField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 145, 30)];          
            [[cell textLabel] setText:@"API Key:"];
            [cell.contentView addSubview:apiKeyField];
        }

    }
    return cell;
}


- (IBAction)segmentChanged:(id)sender {

    [storeFinderTableView reloadData];

}
  • 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-06-16T02:37:02+00:00Added an answer on June 16, 2026 at 2:37 am

    Never mind. Instead of trying to reuse the textFields that I’m adding to my tableView cells, I’ve just created unique textField for each and then test for and remove them based on the segment control.

    Such as:

    if ([mySegmentControl selectedSegmentIndex] == 0) {
    
            if (latField) {
                [latField removeFromSuperview];
                [longField removeFromSuperview];
                [latRadiusField removeFromSuperview];
                [latApiKeyField removeFromSuperview];
            }
    

    Then add the textFields to the cell’s subview and vice versa for segment == 1.

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

Sidebar

Related Questions

I am adding a my header view to self.tableView.tableHeaderView=headerView; This tableView has 10 cells.
I have a tableView which has cells with phone numbers. The app is not
I have a tableview, which has many sections. each section has just one cell.
I have a tableview with custom UITableViewCells, each row in the table has a
I've a dynamic UITableView that I created in Storyboard. This tableview has many cells
I have a simple UITableView with two sections. Each section has a header which
i have a table view which has header for the tableview and another header
Background: I have implemented a standard tableView that has 4 sections. Each of the
I have a dynamic tableview which has a prototype cell with a button that,
I'm building an app with iPod-like controls (play, pause, etc). The app has tableView

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.