iOS7 Tableview Delete Row Best Practice
Asked Answered
C

5

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.

Courbet answered 12/1, 2014 at 9:44 Comment(0)
D
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];
}
Deoxyribose answered 12/1, 2014 at 10:41 Comment(1)
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.Courbet
B
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];
}
}
Branham answered 7/10, 2015 at 12:1 Comment(0)
D
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.

Delly answered 12/1, 2014 at 10:43 Comment(0)
H
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/

Hugohugon answered 12/1, 2014 at 10:8 Comment(2)
Thanks - I'll work my way through that - just what I was looking for but hadn't found.Courbet
you're welcome! If it fixed your problem please upvote & mark the topic as solved. If you have any further trouble just ask.Hugohugon
F
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()
    }
}
Fairish answered 18/3, 2018 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.