Xcode adding new TableView cell adds new cell but shows deleted cell's textfield

41 views Asked by At

I have a tableview that displays cells. Some of these cells have to be removed with a toggle button and then some are added in.

So I have 4 cells in my 3rd section. Their titles in ascending order are follows: same?, start, end, duration. The second and third cells in my list have text fields as accesoryViews and these both get deleted when the toggle button (in the top cell) is turned off. So this leaves me with 2 cells: same? and duration in my 3rd section.

If I press a button it adds in 2 cells to the same section (section 3) but for some reason they have the text fields from the 2 cells that have been deleted as their accessory views.

My code is as follows:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"creatures"];
    if (nil == cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:@"creatures"];
    }
    switch (indexPath.section)
    {
        case 0:
            break;
        case 1:

            break;
        case 2:

            cell.textLabel.text = [timesTitles objectAtIndex:indexPath.row];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            if (indexPath.row == 0) {
                sameTimeEverydaySwitch = [[UISwitch alloc]initWithFrame:CGRectZero];
                [sameTimeEverydaySwitch setOn:YES];
                [sameTimeEverydaySwitch addTarget:self action:@selector(sameEverydaySwitchChanged) forControlEvents:UIControlEventValueChanged];
                cell.accessoryView = sameTimeEverydaySwitch;
            }
            if ([[timesTitles objectAtIndex:indexPath.row] isEqualToString:@"Start time:"]) {

                cell.accessoryView = startTimeTextfield;
                NSLog(@"log");
            }

            if ([[timesTitles objectAtIndex:indexPath.row] isEqualToString:@"End time:"]) {

                cell.accessoryView = endTimeTextfield;
                NSLog(@"log2");

            }

            if ([[timesTitles objectAtIndex:indexPath.row] isEqualToString:@"Meeting duration"]) {

                cell.accessoryView = meetingDurationTextfield;

            }

            break;
        default:
            cell.textLabel.text = @"Not Found";
    }
    return cell;
}

When the button is toggled, I call:

if (![sameTimeEverydaySwitch isOn]) {

    for (int j = 0; j < timesTitles.count; j++) {
        if ([[timesTitles objectAtIndex:j] isEqualToString:@"Start time:"]) {
            [timesTitles removeObjectAtIndex:j];
        }
        if ([[timesTitles objectAtIndex:j] isEqualToString:@"End time:"]) {
            [timesTitles removeObjectAtIndex:j];
        }
    }
        [timesTitles addObjectsFromArray:daysToAddToTitlesArray];


    NSArray *deleteIndexPaths = [NSArray arrayWithObjects:
                                 [NSIndexPath indexPathForRow:2 inSection:2],
                                 [NSIndexPath indexPathForRow:1 inSection:2],
                                 nil];
    NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init];

    for (int i = 2; i < [timesTitles count]; i++) {
        NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:2];
        [insertIndexPaths addObject:path];
    }

    UITableView *tv = (UITableView *)self.view;

    [tv beginUpdates];
    [tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
    [tv deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationFade];
    [tv endUpdates];

}

This code successfully deletes the 2 middle cells.

When I go to add 2 new cells below the remaining 2 using:

    for (int i = 0; i < daysToAddToTitlesArray.count; i++) {

        if (![timesTitles containsObject:[daysToAddToTitlesArray objectAtIndex:i]]) {

            NSIndexPath *path = [NSIndexPath indexPathForRow:[timesTitles count] inSection:2];
            [timesTitles insertObject:[daysToAddToTitlesArray objectAtIndex:i] atIndex:[timesTitles count]];
            [insertIndexPaths addObject:path];

        }
    }
    UITableView *tv = (UITableView *)self.view;
    [tv beginUpdates];
    [tv insertRowsAtIndexPaths:insertIndexPaths withRowAnimation:UITableViewRowAnimationRight];
    [tv endUpdates];

It successfully adds the 2 new cells below but they bot have text fields in them.

The only output for when I run the app is: log and log2.

I'm so confused.

1

There are 1 answers

2
Marko Zadravec On BEST ANSWER

I hope you are familiar with reusable cell concept. (if you are not pleas check this)

In your code I can see that cell.accessoryView are set only if one of the criteria is true, otherwise it take accessory from reusable cell.

Check titles inside array and index (use debug mode)

Marko