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.
So, to remove a cell from a table view you first remove it from your data source, then you call deleteRows(at:) on your table view, providing it with an array of index paths that should be zapped. You can create index paths yourself, you just need a section and row number.
After that, we need to add the row into the table view at index path 0 and remove the values from the input textfield. Run the application and you can add the cell when you tap on Add Button. Now, we will Delete the cell from the tableview when use click on the Delete button on the cell.
I figured it out.
In addition to the aforementioned code, I also need to make changes to the datasource
[items removeObjectAtIndex:0];
- (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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With