DevNull
DevNull

Reputation: 763

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.

Upvotes: 5

Views: 4553

Answers (3)

Hardik Mamtora
Hardik Mamtora

Reputation: 1652

Use like this

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag-1 inSection:0];

CGRect rectOfCellInTableView = [objTableView rectForRowAtIndexPath:indexPath];

CGRect rectOfCellInSuperview = [objTableView convertRect:rectOfCellInTableView toView:[objTableView superview]];

Upvotes: 0

Michael Frederick
Michael Frederick

Reputation: 16714

You need to use this:

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

Upvotes: 2

jscs
jscs

Reputation: 64002

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

Upvotes: 21

Related Questions