0
votes

I am populating a tableview at runtime. I have set an edit option for it where I can delete sections from it. Number of sections depends on an array and there is only one row in a section.

Problem:

When I click the Delete button I receive an exception in log which says:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:974

commitEditingStyle:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
       // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];

        Customer *objCustomer = [arrCustomerList objectAtIndex:indexPath.section];
        NSString *strCusID = [objCustomer customerID];
        CustomerModel *objCustomerModel = [[CustomerModel alloc]init];
        [objCustomerModel deleteCustomer:strCusID];
       [tableView beginUpdates];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];

        [tableView endUpdates];
        [tableView reloadData];

    }   
}  
2
the number of sections returned by the data source can be wronguser756245
you can log it just before the return statement, in numberOfSections...user756245
I am debugging my application. Number of sections always comes correct.Nitish
Can you post the whole error message? It should say with more detail what went wrong.sosborn

2 Answers

1
votes

You can try doing it by placing beginUpdates call at the beginning,

if (editingStyle == UITableViewCellEditingStyleDelete) {
    [tableView beginUpdates];
    Customer *objCustomer = [arrCustomerList objectAtIndex:indexPath.section];
    NSString *strCusID = [objCustomer customerID];
    CustomerModel *objCustomerModel = [[CustomerModel alloc]init];
    [objCustomerModel deleteCustomer:strCusID];
    [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
             withRowAnimation:UITableViewRowAnimationLeft];
    [tableView endUpdates];
    [tableView reloadData];
}

because insert/delete/update opertions must be placed between beginUpdates and endUpdates.

Here's Apple doc :

If you do not make the insertion, deletion, and selection calls inside this block, table attributes such as row count might become invalid.

Also, if the concerned section is the last, make sure you still return 1 for the number of sections. In this case, still return 1 for number of sections and return 0 as the number of rows.

0
votes

Fortunately I found the solution to my problem. Following is the code I used to get rid of the problem:

Customer *objCustomer = [arrCustomerList objectAtIndex:indexPath.section];
NSString *strCusID = [objCustomer customerID];
CustomerModel *objCustomerModel = [[CustomerModel alloc]init];
[objCustomerModel deleteCustomer:strCusID];
[arrCustomerList removeObject:objCustomer];
[tableView reloadData];