How to delete a row in UITableView manually?
26,465
Solution 1
I figured it out.
In addition to the aforementioned code, I also need to make changes to the datasource
[items removeObjectAtIndex:0];
Solution 2
- (IBAction)deleteCustomCellWithUIButton:(id)sender
{
NSLog(@"Message From Custom Cell Received");
NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
NSUInteger row = [indexPath row];
[self.myDataArray removeObjectAtIndex:row];
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
Related videos on Youtube
Comments
-
Anh almost 2 years
Here's what I've come up with:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; // my table view has 2 sections [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
Everytime I build and run, it throws the following exception:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update must be equal to the number of rows contained in that section before the update, plus or minus the number of rows added or removed from that section.
It's a bit confusing. The section is set to 1, yet the exception says it's 0.
-
oberbaum over 14 yearsloved your line NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];. +1 for you ;)
-
-
Sean over 12 yearsFor anyone who had been receiving the error, like I was, stated in the OP; please be sure to alter your data model
before
altering the UITableView's contents. -
thedp over 10 yearsI recommend adding
[self.myTableView beginUpdates];
before you start changing. And[self.myTableView endUpdates];
once you're done.