4

When a cell is in editing mode, and we reload the table view. Is there any way to keep that particular cell still in editing mode showing "Delete" button.

Thanks in advance

1
  • did u solve the issue?
    – iAnurag
    Nov 2, 2015 at 10:48

3 Answers 3

4

Maintain an array of selected items. Don't forget to remove the item when deselecting the rows.

class ViewController: UIViewController {

var selectedData = [IndexPath]()

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.selectedData.append(indexPath)
}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath){
    self.selectedData.removeAll(where: {$0 == indexPath})
}

Reload table view rows while in editing mode and reselect rows programmatically.

self.tableView.beginUpdates()
self.tableView.reloadRows(at: selectedData, with: .automatic)
self.tableView.endUpdates()
for item in selectedData {
    self.tableView.selectRow(at: item, animated: false, scrollPosition: .none)
}

Sample Code Gist

1

You should keep track of cells that entered editing mode. You can do it in your table view delegate method - tableView:willBeginEditingRowAtIndexPath:. I would suggest store all index path of cells currently in editing mode in NSMutableSet.

- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.editingCellIndexPaths addObject:indexPath];
}

- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath {
     [self.editingCellIndexPaths removeObject:indexPath];
}

Than, while reloading, your data source will ask for cells. There you can call - setEditing:animated: method on cells that are in editing mode

- (UITableViewCell *)tableView:(UITableView *)tableView
     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     TableViewCell *cell = //here you get cell
     /*
       cell configure code here
     */

     if ([editingCellIndexPaths containsObject:indexPath]) {
         [cell setEditing:YES animated:NO];
     }
   return cell;
}

Hope that helps

6
  • Thanks but it's not working for me, didEndEditingRowAtIndexPath is getting called when reloading the table Jun 23, 2015 at 9:51
  • Yep, missed that. I would add flag BOOL isReloading and set it to YES before calling reload method on table view. Then check it inside didEndEditingRowAtIndexPath. If YES, don't execute code I proposed and set it to NO; else - execute. Jun 23, 2015 at 9:54
  • I checked it by not removing index path from set in didEndEditingRowAtIndexPath but that's not working too Jun 23, 2015 at 9:56
  • Can you describe "not working"? Is your set empty in cellForRow... or what ? Jun 23, 2015 at 10:01
  • My set contains that particular indexPath and it is going under that "If" condition but still I can't see the delete button Jun 23, 2015 at 10:13
0

Can you verify that you absolutely need to call reloadData when entering edit mode? Usually, it isn't necessary to reload the table's data until the table view will appear. This is because any change that you commit to the model should already be showing in the view, since the user made the changes before the data is saved.

If you need to process some data that is entered, resulting in the saved version being different than the entered version, then I would recommend showing the processed version in the input views before you save the data. This way, you still don't need to reload the data, since the view will already be in sync with the committed version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.