UITableView - 在索引路径问题中插入和删除行

时间:2014-03-13 10:47:24

标签: ios objective-c uitableview

我有一个包含三个部分的tableView。 Section 0有一组固定的单元格,Section 1可以包含不同数量的单元格,Section 2也是固定的。当用户按下Section 2中的最后一个单元格时,我想在Section 1中添加一行。

我的问题是当我点击Section 2中的第一行时,它似乎正确地插入了行。但是当我稍后删除一行时,我不再能够插入项目(didSelectRowAtIndexPath),当我在Section 2的第一行时,它不会被调用。我想这可能是我的reuseIdentifiers的一个问题,但我看不到在哪里。有什么想法吗?

我的tabelView委托方法如下所示:

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

    NSString *cellIdentifier = [self reuseIdentifierForIndexPath:indexPath];

    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell ) {

        cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

        [self configureCell:cell atIndexPath:indexPath];

    }

    // Update cell content..
    [self updateContentOfCell:cell atIndexPath:indexPath];

    return cell;
}


//Helper method to return a reuseIdentifier for TBV cells.
- (NSString *)reuseIdentifierForIndexPath:(NSIndexPath *)indexPath {

    NSString *reuseIdentifier;
    switch (indexPath.section) {
        case 0:
            reuseIdentifier = @"StandardCell";
            break;
        case 1:
            reuseIdentifier = @"SectionCellInWhichIWantToAddMoreCells";
            break;
        case 2:

            switch (indexPath.row) {
                case 0:
                    reuseIdentifier = @"WantToAddCellsToSectionOneByPressingAtThisRow";
                    break;
                case1 :
                    reuseIdentifier = @"OtheerCellInSectionTwo";
                    break;

        }

        break;

        default:
            reuseIdentifier = @"StandardCell";
            break;
    }

    return reuseIdentifier;
}


// Configure cell with type
- (void)configureCell:()cell atIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case 0:
            [self configureFirstSectionCell:cell atIndexPath:indexPath];
            break;
        case 1:
            [self configureFirstSectionCell:cell atIndexPath:indexPath];
            break;
        case 2:
            [self configureSecondSectionCell:cell atIndexPath:indexPath];
            break;
        default:
            break;

    }
}

// Update cell content after reload
- (void)updateContentOfCell:(id)cell atIndexPath:(NSIndexPath *)indexPath {

    // exactly the same structure as configureCell but calls different (updateSecondSectionCell) methods in it's switch cases.
}

- (void)configureFirstSectionCell:(id)cell atIndexPath:(NSIndexPath *)indexPath {

    // Sets initial values of each cell

}


// Where I want to add things...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 2 && indexPath.row == 0) {

        [self.view endEditing:YES];

        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:1] inSection:1];

        [tableView beginUpdates];

        [self.dataSource addObject:@""];
        [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];

}

}

我这样删除:

[self.view endEditing:YES];
[self.tableView beginUpdates];
[self.dataSource removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];

现在发生的事情很奇怪。当我点击Section 2中的第一行时,它似乎正确插入了行。但是当我稍后删除一行时,我不再能够插入项目(didSelectRowAtIndexPath),当我在Section 2的第一行时,它不会被调用。我想这可能是我的reuseIdentifiers的一个问题,但我看不到在哪里。有什么想法吗?

1 个答案:

答案 0 :(得分:-1)

试试这个

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.section == 2 && indexPath.row == 0) {

    [self.view endEditing:YES];
    [self.dataSource addObject:@""];
    [self.tableView reloadData];
}

删除操作代码

-(void)delete
{
[self.view endEditing:YES];
[self.dataSource removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];
}