views:

82

answers:

1

I've searched all over the web, and my code seems to be nearly identical to everything out there, but I can't get my UITableView editing to work for inserting a row. When I click the edit button, all of my existing rows get the delete control, but I get no additional row for insertion. I'm pulling my hair out for what seems like it should be a simple thing. What am I missing?

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    [categoryTV setEditing: editing animated:animated];

    NSIndexPath *ip = [NSIndexPath indexPathForRow:[[appDelegate appCategories] count] inSection:0];

    [self.categoryTV beginUpdates];
        if (editing) {
            [categoryTV insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationLeft];
        } else {
            [categoryTV deleteRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationFade];          
        }
    [self.categoryTV endUpdates];
}

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

 ...

    if (indexPath.row >= [[appDelegate appCategories] count])
        cell.textLabel.text = NSLocalizedString(@"New Category", @"NewCategoryCellText");
    else
        cell.textLabel.text = [[[appDelegate appCategories] objectAtIndex:indexPath.row] detailValue];


    ....

    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.editing) {
        return [[appDelegate appCategories] count] + 1;
    } else {
        return [[appDelegate appCategories] count];
    }
}

As noted, I forgot to include my version of the suggested method, now shown below. Thanks.

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == [[appDelegate appCategories] count]) {
        return UITableViewCellEditingStyleInsert;
    } else {
        return UITableViewCellEditingStyleDelete;
    }
}
+1  A: 

You need to implement the following delegate method to assign an editing style:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleInsert;
}

Obviously you will only return that for the cell you want to have an insert control. Otherwise, you will need to return UITableViewCellEditingStyleDelete.

rickharrison
Thanks. I had implemented a version of the method you suggested, and edited my question to include my version.
Shawn
Are you still seeing just the delete control?
rickharrison
Unfortunately, yes.
Shawn