UITableView Deleteing row error - iphone

I am a newbie to iPhone app development. So please go easy with me :)
I was trying to implement delete row from UItableView when i get this error, which i am not able understand why
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (4), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'
Here is my code to delete item method
-(void)deleteItem:(NSIndexPath *)path{
Item *i = (Item *)[list objectAtIndex:path.row];
NSLog(#"Deleting item [%#]", i.iName);
int ret;
const char *sql = "delete from items where id = ?;";
if (!deleteStmt)
{ // build update statement
if ((ret=sqlite3_prepare_v2(db, sql, -1, &deleteStmt, NULL))!=SQLITE_OK)
{
NSAssert1(0, #"Error building statement to delete items [%s]", sqlite3_errmsg(db));
}
}
// bind values to statement
NSInteger n = i.iId;
sqlite3_bind_int(deleteStmt, 1, n);
// now execute sql statement
if ((ret=sqlite3_step(deleteStmt)) != SQLITE_DONE)
{
NSAssert1(0, #"Error deleting item [%s]", sqlite3_errmsg(db));
}
// now reset bound statement to original state
sqlite3_reset(deleteStmt);
[list removeObjectAtIndex:path.row]; // remove from table
[self readTable]; // refresh array
}
and this is the commitediting style of the UITableView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[appDelegate deleteItem:indexPath];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Can someone please tell me what i am doing wrong. From what i understand the number of rows in the section is not updated. Am i right ??
Thanks in advance.

As stated in the Apple's Table View Programming Guide for iOS the deletion order is relevant:
first delete the item from the table;
second delete it from the model.
So you have to switch the two delete commands:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[appDelegate deleteItem:indexPath];

In my modest opinion and after trying to the same thing, looks like the table still believes the number of rows didn't change, so I make the row hidden and looks like it worked for me.

After some re-looking at the code, figured out the solution myself. The problem was with the the
readTable()
where the older array was not emptied before reading it again. I used the
[list removeAllObjects]
method to empty it. It worked finally. :)

I don't see a [table reloadData]; does it work? Usually it is required.

Related

how to properly update datasource when inserting and deleting rows from UITableView?

I am trying to delete some rows from UITableView and here is my code...
[self.responsesTableView beginUpdates];
[self.responsesTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.processingIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self. responsesTableView endUpdates];
noOfRsponses --;
if (self.segFrndEveryone.selectedSegmentIndex == 0) {
[self.arrFriendComments removeObjectAtIndex:self.processingIndexPath.row];
}else {
[self.arrAllComments removeObjectAtIndex:self.processingIndexPath.row];
}
where one of the two arrays is my datasource depnding on the segment selected.
I am getting the following error and not sure how to deal with this.
The number of rows contained in an existing section after the update
(9) must be equal to the number of rows contained in that section
before the update (9), plus or minus the number of rows inserted or
deleted from that section (0 inserted, 1 deleted) and plus or minus
the number of rows moved into or out of that section (0 moved in, 0
moved out).'
You should update your data source before making manual changes to the UITableView.
// update the data source for the table change
noOfRsponses--;
NSMutableArray *targetArray = (self.segFrndEveryone.selectedSegmentIndex == 0) ? self.arrFriendComments : self.arrAllComments;
[targetArray removeObjectAtIndex:self.processingIndexPath.row];
// update the table view itself
[[self responsesTableView] beginUpdates];
[[self responsesTableView] deleteRowsAtIndexPaths:[NSArray arrayWithObject:self.processingIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[[self responsesTableView] endUpdates];
Are you calling the removeObjectAtIndex on the correct array? Based on the error message, you haven't actually removed an object from the datasource that the tableview is expecting you to have removed from. I'm guessing that maybe you are removing from the wrong array.
And one other thing to try: remove the object first and then do the deleteRowsAtIndexPath.
You should use methods for sections
- (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

UITableView - Remove last cell when there are a certain amount of cells above it

In my UITableView, I have a dedicated cell with an insert control at the bottom to allow the user to insert new rows.
What I want to do is remove/hide this cell when there are a certain number of cells above it (8 in this case).
Here's what I have so far:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 1) {
if ([sites count] == [[BrowserController sharedBrowserController] maximumTabs]) {
return [sites count];
} else {
return [sites count] + 1;
}
} else {
return 1;
}
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
...
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
NSString *newSiteAddress = [NSString stringWithString:#"http://"];
[sites addObject:newSiteAddress];
if ([sites count] == [[%c(BrowserController) sharedBrowserController] maximumTabs]) {
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES];
[[(BookmarkTextEntryTableViewCell *)[tableView cellForRowAtIndexPath:indexPath] textField] becomeFirstResponder];
}
}
Which causes the following exception to be thrown:
2/01/12 4:28:07.956 PM MobileSafari: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(0x2bc0052 0x2d51d0a 0x2b68a78 0x1cf2db 0x747518 0x75282a 0x7528a5 0x812481c 0x75e7bb 0x8b2d30 0x2bc1ec9 0x6c65c2 0x6c655a 0x76bb76 0x76c03f 0x76b2fe 0x984a2a 0x2b949ce 0x2b2b670 0x2af74f6 0x2af6db4 0x2af6ccb 0x491879 0x49193e 0x6c3a9b 0x4430 0x2db5)
Here's a relevant paragraph from Apple's UITableView documentation which has to do with inserting and deleting cells:
Clicking on the insertion or deletion control causes the data source
to receive a tableView:commitEditingStyle:forRowAtIndexPath: message.
You commit a deletion or insertion by calling
deleteRowsAtIndexPaths:withRowAnimation: or
insertRowsAtIndexPaths:withRowAnimation:, as appropriate. Also in
editing mode, if a table-view cell has its showsReorderControl
property set to YES, the data source receives a
tableView:moveRowAtIndexPath:toIndexPath: message. The data source can
selectively remove the reordering control for cells by implementing
tableView:canMoveRowAtIndexPath:.
You should create new methods called "insertLastCell" and "deleteLastCell" (or something along those lines) which explicitly tells your table view to insert and delete that last cell via insertRowsAtIndexPaths:withRowAnimation: and deleteRowsAtIndexPaths:withRowAnimation:.
Once the insert and deletes are "committed", only then can you report back different numbers in your numberOfRowsInSection method.

problem while deleting the row of a grouped table

hii every one
i am using following code to delete a row in the rgouped table view
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Get the object to delete from the array.
insertUpdateDelete *objInsertUpdateDelete = [appDelegate.arrObjects objectAtIndex:indexPath.row];
[appDelegate DeleteRecord:objInsertUpdateDelete];
//Delete the object from the table.
[tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}
but its crashing at the line
[tableView1 deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
crash log is:
2011-05-20 19:21:20.233
iICS[10744:207] * Assertion failure
in -[UITableView
_endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1448.89/UITableView.m:995
2011-05-20 19:21:20.235 iICS[10744:207]
*** Terminating app due to uncaught exception
'NSInternalInconsistencyException',
reason: 'Invalid update: invalid
number of rows in section 0. The
number of rows contained in an
existing section after the update (3)
must be equal to the number of rows
contained in that section before the
update (3), plus or minus the number
of rows inserted or deleted from that
section (0 inserted, 1 deleted).'
can any buddy help me? thanx in advance
Make sure that your data source is reflecting the changes you are making before calling deleteRowsAtIndexPaths:. I see that you are calling [appDelegate DeleteRecord:objInsertUpdateDelete]; but if your data source is not getting its records directly from the appDelegate and getting them from maybe an array in your class you also need to remove it from that array.
As a side note your design pattern for using the app delegate may be like a "ball of mud". Is it good practice to use AppDelegate for data manipulation and Handling?
You need to first check if the row you're deleting is the only row in a section.
Look in your method:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
Does this method always return the correct row count? It seems you're NOT removing the row from whatever datasource is backing your section.
EDIT:
It's possible you are doing the delete, but it has not completed (or committed) at the time your table's dataSource is asking for the new rowcount. You might need to delay your [tableView deleteRowsAtIndexPath:] until the database commits.

Crash when deleting row - Inconsistency in section not being updated

When I delete a row from UITableView using commitEditingStyle, my app crashes with the following error message. The odd thing though is I am deleting from section 3. The inconsistency according to the message is from section 4.
* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:920
2010-11-22 19:56:44.789 bCab[23049:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 4. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).'
In datasource, I update section 4 depending on the number of rows in section 3. When a row is deleted from section 3, number of rows in section 4 goes from 0 to 1. This seems to cause the issue. Is there no way to avoid this?
Any pointers would be highly appreciated.
Thanks.
UPDATE:
numberOfSectionsInTableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 6;
}
numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
bCabAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
if (section == 0) { // First name, last name
return 2;
}
else if (section == 1) { // Password
return 2;
}
else if (section == 2) { // Mobile, DOB , Gender
return 3;
}
else if (section == 3) { // credit cards
return [creditCards count];
}
else if (section == 4) { // Add credit card
if ([creditCards count] >= 3) {
return 0;
}
else {
return 1;
}
}
else if (section == 5) {
return 0;
}
return 0;
}
commitEditingStyle
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
if (indexPath.section == 3) {
// Delete the row from the data source
NSLog(#"%d %d", indexPath.section, indexPath.row);
[creditCards removeObjectAtIndex:indexPath.row];
// Delete from backend
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//[tableView reloadData];
}
}
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
}
}
I have tried with and without [tableView reloadData] with same results.
Thanks for trying to help!
In datasource, I update section 4
depending on the number of rows in
section 3. When a row is deleted from
section 3, number of rows in section 4
goes from 0 to 1. This seems to cause
the issue. Is there no way to avoid
this?
When using deleteRowsAtIndexPaths:withAnimation you are guaranteeing that the data source will have removed just the rows with the specified index paths. In your case you are also inserting a row into the table which means that state of the data source is not what the table view expects.
When deleting a row in section 3 that also involves inserting a row in section 4 you must do something like:
[self.tableView beginUpdates];
[self.tableView [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPath:[NSArray arrayWithObject:indexPathForInsertedRow] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
Before deleting the cell, do you remove the corresponding object in creditCards ?
How do you add the new row in section 4 when a row in section 3 is deleted ?
It is not a good solution, but you can try to make a reloadData when row is deleted.

UITableView: deleting sections with animation

Update
I have posted my solution to this problem as an answer below. It takes a different approach from my first revision.
Original Question
I previously asked a question on SO that I thought solved my issues:
How to deal with non-visible rows during row deletion. (UITableViews)
However, I now have similar problems again when removing sections from a UITableView.
(they resurfaced when I varied the number of sections/rows in the table).
Before I lose you because of the shear length of my post, let me state the problem clearly, and you can read as much as you require to provide an answer.
Problem:
If batch deleting rows AND sections from a UITableView, the application crashes, sometimes. It depends on the configuration of the table and the combination of rows and sections I choose to remove.
The log says I crashed because it says I have not updated the datasource and the table properly:
Invalid update: invalid number of rows in section 5. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).
Now quickly, before you write the obvious answer, I assure you I have indeed added and deleted the rows and sections properly from the dataSource. The explanation is lengthy, but you will find it below, following the method.
So with that, if you are still interested…
Method that handles removal of sections and rows:
- (void)createFilteredTableGroups{
//index set to hold sections to remove for deletion animation
NSMutableIndexSet *sectionsToDelete = [NSMutableIndexSet indexSet];
[sectionsToDelete removeIndex:0];
//array to track cells for deletion animation
NSMutableArray *cellsToDelete = [NSMutableArray array];
//array to track controllers to delete from presentation model
NSMutableArray *controllersToDelete = [NSMutableArray array];
//for each section
for(NSUInteger i=0; i<[tableGroups count];i++){
NSMutableArray *section = [tableGroups objectAtIndex:i];
//controllers to remove
NSMutableIndexSet *controllersToDeleteInCurrentSection = [NSMutableIndexSet indexSet];
[controllersToDeleteInCurrentSection removeIndex:0];
NSUInteger indexOfController = 0;
//for each cell controller
for(ScheduleCellController *cellController in section){
//bool indicating whether the cell controller's cell should be removed
NSString *shouldDisplayString = (NSString*)[[cellController model] objectForKey:#"filteredDataSet"];
BOOL shouldDisplay = [shouldDisplayString boolValue];
//if it should be removed
if(!shouldDisplay){
NSIndexPath *cellPath = [self indexPathOfCellWithCellController:cellController];
//if cell is on screen, mark for animated deletion
if(cellPath!=nil)
[cellsToDelete addObject:cellPath];
//marking controller for deleting from presentation model
[controllersToDeleteInCurrentSection addIndex:indexOfController];
}
indexOfController++;
}
//if removing all items in section, add section to removed in animation
if([controllersToDeleteInCurrentSection count]==[section count])
[sectionsToDelete addIndex:i];
[controllersToDelete addObject:controllersToDeleteInCurrentSection];
}
//copy the unfiltered data so we can remove the data that we want to filter out
NSMutableArray *newHeaders = [tableHeaders mutableCopy];
NSMutableArray *newTableGroups = [[allTableGroups mutableCopy] autorelease];
//removing controllers
int i = 0;
for(NSMutableArray *section in newTableGroups){
NSIndexSet *indexesToDelete = [controllersToDelete objectAtIndex:i];
[section removeObjectsAtIndexes:indexesToDelete];
i++;
}
//removing empty sections and cooresponding headers
[newHeaders removeObjectsAtIndexes:sectionsToDelete];
[newTableGroups removeObjectsAtIndexes:sectionsToDelete];
//update headers
[tableHeaders release];
tableHeaders = newHeaders;
//storing filtered table groups
self.filteredTableGroups = newTableGroups;
//filtering animation and presentation model update
[self.tableView beginUpdates];
tableGroups = self.filteredTableGroups;
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationTop];
[self.tableView deleteRowsAtIndexPaths:cellsToDelete withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
//marking table as filtered
self.tableIsFiltered = YES;
}
My guess:
The problem seems to be this: If you look above where I list the number of cells in each section, you will see that section 5 appears to increase by 1. However, this is not true. The original section 5 has actually been deleted and another section has taken its place (specifically, it is old section 10).
So why does the table view seem not to realize this? It should KNOW that I removed the old section and should not expect a new section that is now located at the old section's index to be bound by the deleted section's number of rows.
Hopefully this makes sense, it is a little complicate to write this out.
(note this code worked before with a different number of rows/sections. this particular configuration seems to give it issues)
I’ve run into this problem before. You are trying to delete all rows from a section and then, in addition, that now empty section. However, it is sufficient (and proper) to remove that section only. All rows within it will be removed as well. Here is some sample code from my project that handles deletion of one row. It needs to determine whether it should remove only this row from a section or delete the entire section if it is the last remaining row in that section:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// modelForSection is a custom model object that holds items for this section.
[modelForSection removeItem:[self itemForRowAtIndexPath:indexPath]];
[tableView beginUpdates];
// Either delete some rows within a section (leaving at least one) or the entire section.
if ([modelForSection.items count] > 0)
{
// Section is not yet empty, so delete only the current row.
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
else
{
// Section is now completely empty, so delete the entire section.
[tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section]
withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
}
}
I notice that you're deleting the sections from the table first, and then deleting rows.
I know there's a complicated discussion of batch insertion and deletion for UITableViews in the Table View Programming Guide, but it doesn't specifically cover this.
I think what's happening is that deleting the sections is causing the row deletions to refer to the wrong row.
i.e. you want to delete section #2 and row #1 from section #4... but after you've deleted section #2, the old section #4 is now the third section, so you when you delete with the old NSIndexPath of (4, 1) you're deleting some random different row that may not exist.
So I think the fix might be as simple as swapping those two lines of code, so you're deleting the rows first, then the sections.
So finally here is my solution to this issue.
This method can be applied to tables of any size, any number of sections (as far as I can tell)
As before I have modified Matt Gallagher's tableview Code which places cell-specific logic in a separate cell controller. However, you can easily adapt this method to a different model
I have added the following (relevant) ivars to Matt's code:
NSArray *allTableGroups; //always has a copy of every cell controller, even if filtered
NSArray *filteredTableGroups; //always has a copy of the filtered table groups
Matt's original ivar:
NSArray *allTableGroups
…always points to one of the above arrays.
This can probably be refactored and improved significantly, but I haven't had the need. Also, if you use Core Data, NSFetchedResultsController makes this easier.
Now on to the method (I am trying to comment as much as I can):
- (void)createFilteredTableGroups{
//Checking for the usual suspects. all which may through an exception
if(model==nil)
return;
if(tableGroups==nil)
return;
if([tableGroups count]==0)
return;
//lets make a new array to work with
NSMutableArray *newTableGroups = [[allTableGroups mutableCopy] autorelease];
//telling the table what we are about to do
[self.tableView beginUpdates];
//array to track cells for deletion animation
NSMutableArray *indexesToRemove = [NSMutableArray array];
//loop through each section
for(NSMutableArray *eachSection in tableGroups){
//keeping track of the indexes to delete for each section
NSMutableIndexSet *indexesForSection = [NSMutableIndexSet indexSet];
[indexesForSection removeAllIndexes];
//increment though cell indexes
int rowIndex = 0;
//loop through each cellController in the section
for(ScheduleCellController *eachCellController in eachSection){
//Ah ha! A little magic. the cell controller must know if it should be displayed.
//This you must calculate in your business logic
if(![eachCellController shouldDisplay]){
//add non-displayed cell indexes
[indexesForSection addIndex:rowIndex];
}
rowIndex++;
}
//adding each array of section indexes, EVEN if it is empty (no indexes to delete)
[indexesToRemove addObject:indexesForSection];
}
//Now we remove cell controllers in newTableGroups and cells from the table
//Also, each subarray of newTableGroups is mutable as well
if([indexesToRemove count]>0){
int sectionIndex = 0;
for(NSMutableIndexSet *eachSectionIndexes in indexesToRemove){
//Now you know why we stuck the indexes into individual arrays, easy array method
[[newTableGroups objectAtIndex:sectionIndex] removeObjectsAtIndexes:eachSectionIndexes];
//tracking which cell indexPaths to remove for each section
NSMutableArray *indexPathsToRemove = [NSMutableArray array];
int numberOfIndexes = [eachSectionIndexes count];
//create array of indexPaths to remove
NSUInteger index = [eachSectionIndexes firstIndex];
for(int i = 0; i< numberOfIndexes; i++){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:sectionIndex];
[indexPathsToRemove addObject:indexPath];
index = [eachSectionIndexes indexGreaterThanIndex:index];
}
//delete the rows for this section
[self.tableView deleteRowsAtIndexPaths:indexPathsToRemove withRowAnimation:UITableViewRowAnimationTop];
//next section please
sectionIndex++;
}
}
//now we figure out if we need to remove any sections
NSMutableIndexSet *sectionsToRemove = [NSMutableIndexSet indexSet];
[sectionsToRemove removeAllIndexes];
int sectionsIndex = 0;
for(NSArray *eachSection in newTableGroups){
//checking for empty sections
if([eachSection count]==0)
[sectionsToRemove addIndex:sectionsIndex];
sectionsIndex++;
}
//updating the table groups
[newTableGroups removeObjectsAtIndexes:sectionsToRemove];
//removing the empty sections
[self.tableView deleteSections:sectionsToRemove withRowAnimation:UITableViewRowAnimationTop];
//updating filteredTableGroups to the newTableGroups we just created
self.filteredTableGroups = newTableGroups;
//pointing tableGroups at the filteredGroups
tableGroups = filteredTableGroups;
//invokes the animation
[self.tableView endUpdates];
}
I saw this same exact error as the result of prematurely releasing the background view of my custom tableview cell.
With NSZombieEnabled I got a an exception being thrown way down below an internal call to a function to prepare the cell for reuse. Without NSZombieEnabled, I was getting the Internal consistency error.
Incidentally when I fixed the retain/release issue on the background view of the cell, I was able to delete the last row of the section without having to delete the section explicitly.
Moral of the story: This error just means something bad is happening when you try to delete, and one of the things that happens when you delete is the cell gets prepared for reuse, so if you are doing anything custom with your tableview cells, look for a possible error there.
I suspect that you are forgetting to remove the object representing the section from your internal storage, so that the -numberOfSectionsInTableView: method is still returning 1 after all sections are deleted.
That's exactly what I was doing wrong when I had the same crash!
A much simpler way to address this is to update your data source, then call reloadSections
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
This will reload a single section. Alternatively you could use indexSetWithIndexesInRange: to reload multiple sections simultaneously.
or just do this
- (void)tableView:(UITableView *)tv
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the object from the table.
[directoriesOfFolder removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
}
}
directories of folder being your Array! Thats is all above codes didnt work for me! This is less expensive to do and just makes sense!