Xcode添加新的TableView单元格会添加新单元格但会显示已删除单元格的文本字段

时间:2015-06-15 19:29:38

标签: uitableview cell textfield reuseidentifier

我有一个显示单元格的tableview。必须使用切换按钮删除其中一些单元格,然后添加一些单元格。

所以我的第3部分有4个细胞。他们的标题按升序排列如下:相同?,开始,结束,持续时间。我列表中的第二个和第三个单元格的文本字段为accesoryViews,当关闭切换按钮(在顶部单元格中)时,这两个单元格都会被删除。所以这给我留下了2个细胞:同样的?和我的第3部分的持续时间。

如果我按下一个按钮,它会将2个单元格添加到同一部分(第3部分),但由于某种原因,他们将2个单元格中的文本字段作为附件视图删除。

我的代码如下:

- (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;
}

当切换按钮时,我打电话:

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];

}

此代码成功删除了2个中间单元格。

当我使用以下方法在剩下的2个下面添加2个新单元格时:

    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];

它成功添加了下面的2个新单元格,但它们中包含文本字段。

运行应用程序时唯一的输出是:log和log2。

我很困惑。

1 个答案:

答案 0 :(得分:0)

我希望你熟悉可重复使用的细胞概念。 (如果您不是请求检查this

在您的代码中,我可以看到只有在其中一个条件为真时才设置cell.accessoryView,否则它会从可重复使用的单元格中获取附件。

检查数组和索引中的标题(使用调试模式)

马尔科