Ammad
Ammad

Reputation: 23

crash application while delete section from UITableView

In tableview I have many sections, on the top row of section I have a delete button, I want to do delete section after clicking button but its crashing please help me how can I do this?? here is the code...

//****** adding delete section button
     CellButton *_sectionButton = (CellButton*)[cell viewWithTag:10];
     _sectionButton.hidden = YES;
     _sectionButton.indexPath = indexPath;
     [_sectionButton addTarget:self action:@selector(sectionDeleteButton:)      forControlEvents:UIControlEventTouchUpInside];

-(void)sectionDeleteButton: (id)sender
{
   CellButton *_secbtn = (CellButton*)sender;
   NSInteger sectionNumber = _secbtn.indexPath.section;
   [self.tableView beginUpdates];
   [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionNumber] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
    [self.tableView reloadData];

}

EDIT: The error messsage is:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2380.17/UITableView.m:1054 2013-07-04 04:25:15.921 estimation[9025:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 1 deleted)

Upvotes: 2

Views: 8636

Answers (4)

Dipak Narigara
Dipak Narigara

Reputation: 1806

You have to manage your number of sections in numberOfSectionsInTableview: delegate method

Upvotes: 1

ChenYilong
ChenYilong

Reputation: 8673

I met this problem, but the reason is different from yours.

When I add a section for the tableView, I reload data with this method, this also crash logging like yours:

[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows] withRowAnimation:UITableViewRowAnimationNone];

Why do I use this method to reload my table? If I only refresh cells, this works fine, but I forgot I want to change the sections. For more information: What is More reliable way to reload UITableView Data?

Upvotes: 0

Gank
Gank

Reputation: 4667

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return  [MLControl shared].arrBuyList.count;//1;//[MLControl shared].arrBuyList.count;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
       // [tableView beginUpdates];
        NSLog(@"indexPath.row=%ld,indexPath.se=%ld",(long)indexPath.row,(long)indexPath.section);
        [[MLControl shared].arrBuyList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    //    [tableView endUpdates];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

Upvotes: 0

allprog
allprog

Reputation: 16790

I guess the numberOfSectionsInTableView: returns the previous number of sections. You must make sure that what you tell the table view to do also happens in the data source. That is, if you delete a section, then the numberOfSectionsInTableView must also return a number one less than before. The same stands for cells and everything else. This is probably described in the error you get.

Upvotes: 6

Related Questions