Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get deleteRowsAtIndexPaths to work

I have a UITableView and a scheduler running in the background deleting records from the underlying data object. Every time an object is deleted I want the table view to update accordingly.

But i keep getting this annoying error message when trying to delete a row from the table view.

* Terminating app due to uncaught exception NSInternalInconsistencyException, reason: "Invalid index path for use with UITableView. Index paths passed to table view must contain exactly two indices specifying the section and row. Please use the category on NSIndexPath in UITableView.h if possible."

The code:

NSIndexPath *indexPath = [NSIndexPath indexPathWithIndex:0];
NSUInteger row = [indexPath row];

NSMutableArray* files = [[NSMutableArray alloc] initWithArray:fileNames];
[files removeObjectAtIndex:[indexPath row]];
[fileNames dealloc];
fileNames = [NSArray arrayWithArray:files];

//[self.fileNames removeObjectAtIndex:row];
[self.tableDraft deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];

PS: I have also tried [tableView reloadData]; but that has undesired side effects, erasing the selected index.

like image 931
DevNull Avatar asked Jan 07 '12 23:01

DevNull


2 Answers

Have you considered following the message's advice? Your index path does not contain both a row and a section component. Use +[NSIndexPath indexPathForRow:inSection:] to create the index path object, so that it has the information the table view requires:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
like image 195
jscs Avatar answered Nov 19 '22 09:11

jscs


You need to use this:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowIndex inSection:sectionIndex];

like image 39
Michael Frederick Avatar answered Nov 19 '22 09:11

Michael Frederick