-1

I'm using a TableView inside a ViewController. There are two scenarios, one where there are 3 sections and one where there are 4.

When the app loads the default is 3. One of the TableViewCells shows another ViewController in which the user makes a choice. Then using the back button they return to the opening VC where the table is reloaded.

The issue comes when a user moves from 4 sections back to 3. As you can see in the code below the 3rd section (case 2) implements a textField when showing 4 sections. When the user moves back to 3 the textField remains. I assumed the table would start from scratch when reloaded. Instead it keeps the previous settings and adds the new settings over the top.

Any ideas how to stop this from happening? some kind of reset?

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

{
    UITableViewCell *serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell"];



        if (sections == 3) {
            switch (indexPath.section) {
                case 0:
                    if ([testLocation isEqualToString:@""]) {
                        serverLocCell.textLabel.text = @"Choose location";
                    }
                    else {
                        serverLocCell.textLabel.text = testLocation;
                    }

                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

                    break;
                case 1:
                    serverLocCell.textLabel.text = testType;
                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case 2:
                    serverLocCell.textLabel.text = @"Start Test";
                    serverLocCell.textLabel.textColor = [UIColor whiteColor];
                    serverLocCell.textLabel.textAlignment = NSTextAlignmentCenter;
                    serverLocCell.detailTextLabel.text = @"";
                    serverLocCell.accessoryType = UITableViewCellAccessoryNone;
                    serverLocCell.backgroundColor = [UIColor colorWithRed:0.0 / 255 green:102.0 / 255 blue:51.0 / 255 alpha:1.0];
                    break;
                default:
                    break;
            }
        }
        else {
            switch (indexPath.section) {
                case 0:
                    if ([testLocation isEqualToString:@""]) {
                        serverLocCell.textLabel.text = @"Choose location";
                    }
                    else {
                        serverLocCell.textLabel.text = testLocation;
                    }

                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

                    break;
                case 1:
                    serverLocCell.textLabel.text = testType;
                    serverLocCell.detailTextLabel.text = @"Change";
                    serverLocCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                    break;
                case 2:
                    if ([testType isEqualToString:@"Published App Test"]) {

                        publishedAppTextField = [[UITextField alloc]init];
                        publishedAppTextField.tag = 1;
                        [publishedAppTextField addTarget:self
                                                  action:@selector(textFieldDidChange:)
                                        forControlEvents:UIControlEventEditingChanged];

                        serverLocCell.textLabel.text = @"";
                        serverLocCell.accessoryType = UITableViewCellAccessoryNone;
                        publishedAppTextField.frame = CGRectMake(7, 10, 300, 30);
                        publishedAppTextField.clearsOnBeginEditing = YES;
                        publishedAppTextField.delegate = self;
                        publishedAppTextField.returnKeyType = UIReturnKeyDone;
                        [serverLocCell setSelectionStyle:UITableViewCellSelectionStyleNone];
                        [serverLocCell.contentView addSubview:publishedAppTextField];
                        serverLocCell.backgroundColor = [UIColor whiteColor];
                        break;
                    }
                    else {

                        break;
                    }
                case 3:
                    serverLocCell.textLabel.text = @"Start Test";
                    serverLocCell.textLabel.textColor = [UIColor whiteColor];
                    serverLocCell.textLabel.textAlignment = NSTextAlignmentCenter;
                    serverLocCell.detailTextLabel.text = @"";
                    serverLocCell.accessoryType = UITableViewCellAccessoryNone;
                    serverLocCell.backgroundColor = [UIColor colorWithRed:0.0 / 255 green:102.0 / 255 blue:51.0 / 255 alpha:1.0];
                    break;
                default:
                    break;
            }

        }
    return serverLocCell;
}

Thanks,

Nick
  • 1,417
  • 1
  • 14
  • 21
Dan
  • 2,304
  • 6
  • 42
  • 69

1 Answers1

1

What is happening is likely due to using the same cell identifier. Use two different cellIdentifiers for each scenario you have (3 and 4 sections). So it may look something like this:

if (sections == 3) {
    serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell1"];
    ...
} else if (sections == 4) {
    serverLocCell = [tableView dequeueReusableCellWithIdentifier:@"homeCell2"];
    ...
}

hope this helps

KDaker
  • 5,899
  • 5
  • 31
  • 44