18010

UITableView Deleteing row error

Question:

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.

Answer1:

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

Answer2:

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.

Answer3:

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. :)

Answer4:

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

Recommend

  • How to fetch data from pList in Label
  • Creating a file name by adding two NSStrings
  • Load audio in DOCUMENTS when UITableView “cell” is pressed
  • Adding custom gesture to iCarousel items
  • Date formatter for converting 14-sept-2011 in 14th Sept
  • TouchesEnded not working in iOS5, working fine in iOS4
  • Store and fetch documents in/from app documents folder
  • NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for em
  • Display Local HTML file from documents directory in a UIWebView on iPhone
  • -[__NSArrayM insertObject:atIndex:]: object cannot be nil
  • UITableView In Edit Mode - Pressing Delete Makes My App Crash
  • Why won't my UITableViewCell deselect and update its text?
  • .NET video play library which allows to change the playback rate?
  • How to 'create temp table as select' in Slick?
  • ListItem.Attributes.Add not working
  • pyodbc doesn't report sql server error
  • MySQL Order by column = x, column asc?
  • Saving Changes After In-App Purchase Has Been Purchased
  • QLPreviewController hide print button in ios6
  • VSO Build — Response status code does not indicate success: 404 (Not Found)
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Convert array of 8 bytes to signed long in C++
  • Weird JavaScript statement, what does it mean?
  • php design question - will a Helper help here?
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • -fvisibility=hidden not passed by compiler for Debug builds
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • LevelDB C iterator
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?