Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reorder table view cell without the delete button and implement swipe to delete for rows

I read about table view and it's editing styles but I have some problems as there is only three editing style as follow:

  1. UITableViewCellEditingStyleNone
  2. UITableViewCellEditingStyleDelete
  3. UITableViewCellEditingStyleInsert

I want to have reordering the tableview cells which I implemented successfully using it's delegates.

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

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
   return UITableViewCellEditingStyleNone;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   //handle the editing style
}
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
  //move cells
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

I don't want to use UITableViewCellEditingStyleDelete as it shows a red circular button on table view. Instead of this I want swipe-to-delete and the reordering functionality together.

Is there any way to implement this?

like image 860
Niloufar Avatar asked Aug 02 '15 13:08

Niloufar


1 Answers

This can be done but with the following conditions. The swipe-to-delete will only work while the table view is not in edit mode and the table re-ording will only work while the table view is in edit mode. You will not be able to have swipe-to-delete work at the same time as table re-ordering.

To make this work you need the following:

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

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
   // Only allow deletion when the table isn't being edited
   return tableView.isEditing ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   // handle the row deletion
}

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    // Check if move is valid
    return proposedDestinationIndexPath;
}

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

- (void)tableView:(UITableView * nonnull)tableView moveRowAtIndexPath:(NSIndexPath * nonnull)fromIndexPath toIndexPath:(NSIndexPath * nonnull)toIndexPath {
    // process the moved row
}

You will need the standard Edit button in the navigation bar (or some other way to toggle the table's edit mode). A common way to do this is to add the following line inside your table view controller's viewDidLoad method:

self.navigationItem.rightBarButtonItem = [self editButtonItem];
like image 95
rmaddy Avatar answered Sep 28 '22 07:09

rmaddy