iOS7 Tableview Delete Row Best Practice

2019-02-09 19:14发布

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.

5条回答
Explosion°爆炸
2楼-- · 2019-02-09 19:49

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/

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-02-09 19:50

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];
}
}
查看更多
等我变得足够好
4楼-- · 2019-02-09 19:53

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];
}
查看更多
Emotional °昔
5楼-- · 2019-02-09 20:00

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()
    }
}
查看更多
叼着烟拽天下
6楼-- · 2019-02-09 20:02

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.

查看更多
登录 后发表回答