开发者

Exception while deleting section from UITableView

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 *objCust开发者_StackOverflow中文版omerModel = [[CustomerModel alloc]init];
        [objCustomerModel deleteCustomer:strCusID];
       [tableView beginUpdates];
            [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];

        [tableView endUpdates];
        [tableView reloadData];

    }   
}  


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜