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.

- (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];
}
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:
Then add the textFields to the cell’s subview and vice versa for segment == 1.