6

enter image description here

On the iPhone there is a text book example of how to delete a tableview row in the Message Apps. This appears to use three separate views to perform the task.

My question is about whether there are short cuts to achieve this or do you just create three screens and to do the bleedin' obvious.

Many thanks.

Edward Hasted
  • 3,201
  • 8
  • 30
  • 48

5 Answers5

12

Removing a row from storyboard is pretty straightforward. You just have to inherit 2 methods in your TableView Data source. First is telling if a row can be deleted:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

Second is removing the row from table view:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
Damien
  • 660
  • 6
  • 15
  • It's not the coding to delete a row that was confusing me but the way the view(s) are presented to the user. Do you use a number or can you use one. – Edward Hasted Jan 12 '14 at 10:49
7

You have to implement the necessary UITableViewDelegate and UITableViewDataSource methods.

First, add this:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{
if (editingStyle == UITableViewCellEditingStyleDelete) 
{
    [self.dataArray removeObjectAtIndex:indexPath.row];
    [tableView reloadData];
}
}
Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
4

Following steps you should follow when deleting any row from a tableView:

  1. Get the indexPath of row to be deleted.

  2. Remove the row from data model of the tableView DataSource.

[yourDataModel removeObjectAtIndex:indexPath.row];

  1. Update the screen by reloading tableView.

[tableView reloadData];

Let me know if more info needed.

Vineet Singh
  • 4,009
  • 1
  • 28
  • 39
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
3

I'm not really sure what you mean with three different views, but this would be the solution by an example to delete a row from UITableView:

http://www.appcoda.com/model-view-controller-delete-table-row-from-uitableview/

fahu
  • 1,511
  • 4
  • 17
  • 26
  • Thanks - I'll work my way through that - just what I was looking for but hadn't found. – Edward Hasted Jan 12 '14 at 10:40
  • you're welcome! If it fixed your problem please upvote & mark the topic as solved. If you have any further trouble just ask. – fahu Jan 12 '14 at 10:55
2

Here's how to do this in Swift (4):

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if(editingStyle == UITableViewCellEditingStyle.delete){
        dataArray.remove(at: indexPath.row)
        tableView.reloadData()
    }
}
Gabriel Garrett
  • 2,087
  • 6
  • 27
  • 45