Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a row in UITableView manually?

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.

like image 382
Anh Avatar asked May 27 '09 10:05

Anh


People also ask

How do I delete a cell in UITableView?

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.

How do I delete a simple tableView in swift 5?

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.


2 Answers

I figured it out.

In addition to the aforementioned code, I also need to make changes to the datasource

[items removeObjectAtIndex:0];
like image 90
Anh Avatar answered Oct 01 '22 13:10

Anh


- (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];
}
like image 39
Christian Loncle Avatar answered Oct 01 '22 15:10

Christian Loncle