6

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Niloufar
  • 512
  • 1
  • 5
  • 24
  • Have you done this. I need to do the same – Parul Garg Nov 19 '15 at 06:38
  • @ParulGarg sorry for late reply. Yes, please check out this: http://stackoverflow.com/questions/31802088/reorder-tableview-cell-with-a-custom-button – Niloufar Dec 07 '15 at 11:35
  • FYI a smart cookie figured a work around to this http://stackoverflow.com/questions/6097464/reordering-controls-on-uitableview-while-not-in-editing-mode/33182901#33182901 – Tys Mar 13 '17 at 12:43

1 Answers1

1

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];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks for your reply, but iTunes have this functionality so I guess it should be available for us as well. – Niloufar Aug 03 '15 at 06:30
  • iTunes is on your Mac, not your iOS device. Please clarify what you mean. – rmaddy Aug 03 '15 at 06:32
  • I mean apple Music on iOS 8.4 has this ability – Niloufar Aug 03 '15 at 06:38
  • Which screen in the Music app? – rmaddy Aug 03 '15 at 18:14
  • When you play a song and in the player you click on the button to go and see the queue – Niloufar Aug 04 '15 at 05:23
  • Dear toady, I managed to do something like that by using the code on this link: https://github.com/adamraudonis/UITableViewCell-Swipe-for-Options. But now I have the problem of positioning the reorder button in the correct place like Apple Music – Niloufar Aug 04 '15 at 05:24
  • As @Tys mentioned above, there's an answer that allows you to have swipe-to-delete and reordering without changing modes: https://stackoverflow.com/questions/6097464/reordering-controls-on-uitableview-while-not-in-editing-mode – Adrian Nov 04 '17 at 20:27