0
votes

I'm trying to use an extra row at the bottom of my UITableView which would be used to insert a record. This works fine, but I'm having trouble getting it to animate properly. Either the extra row is shown properly and I get no animation (by using [self.tableView reloadData] in the setEditing: method. Or, I get animation and no extra row by not using reloadData. I know that I'm probably not showing the extra row properly (returning one more row in my numberOfRowsInSection method, and using reloadData to force a refresh.).

I know there must be a right way to do this, but I haven't seen any examples.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {


    [super setEditing:editing animated:animated];

    [self.tableView setEditing:editing animated:animated];


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:section];
    if (self.editing == YES) {
        return [sectionInfo numberOfObjects] + 1;    
    }
    return [sectionInfo numberOfObjects];

}


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row + 1 > [fetchedResultsController.fetchedObjects count]) {
        cell.textLabel.text = @"Add New Row...";
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    } else {

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        MyManagedObject *aManagedObject = [fetchedResultsController.fetchedObjects objectAtIndex:indexPath.row];
        cell.textLabel.text = aManagedObject.planName;
    }
}
2

2 Answers

2
votes

Use insertRowsAtIndexPaths:withRowAnimation: to inform the table view that you "inserted" a row at the necessary location when editing begins and deleteRowsAtIndexPaths:withRowAnimation: to inform the table view that you "removed" the row when editing is complete.

0
votes

This is the kind of UI need that you should really use a tableFooterView for. That puts a custom view at the foot of the table but it does not affect the row count of the table itself. If you insert and actual row that is not reflected in the data, you have to micromanage all the functions related to row count because the rows in data no longer sync with the rows in the tableview.