2
votes
  • When my table view is in edit mode, the red (-) buttons appear.
  • When the user taps one of them the [Delete] button appears.
  • When the user taps on [Delete] I first check a few things (partly online). This delete may not be allowed.

  • When deleting that cell is not allowed, how do I hide the [Delete] button and let the red (|) button become a (-) again in an animated way? So, I don't want my whole table to leave editing state.

4
Can you show some code?Abdullah Shafique
Do you need to allow any editing at all? Sounds like you just need to disable edit mode and your problem is solved. Try: [self setEditing:NO animated:YES] (where 'self' is your UITableView instance)MystikSpiral
@AbdullahShafique I'm using a NSFetchedResultsController and would have to post non-related code. The deletion process described is mainly handled by the UITableView.meaning-matters
@MystikSpiral No. A user may want to delete another cell as well, and I don't want to force him having to tap Edit on the navbar again.meaning-matters

4 Answers

7
votes

To get the actual animation (Instead of the UITableViewRowAnimationRight/UITableViewRowAnimationAutomatic) animations, just do

[self.tableView beginUpdates];
[self.tableView setEditing:NO animated:NO];
[self.tableView setEditing:YES animated:NO];
[self.tableView endUpdates];

beginUpdates and endUpdates provide the animation, and the tableView is just switched from not editing to editing instantly, which closes the delete button.

Hope this helps!

7
votes

I've run into this issue myself where I may bring up an alert view to prompt the user further and wish to reset the delete button if they choose not to proceed. This seems like the easiest approach, assuming deleteIndexPath is the index path of the row selected for deletion:

[self.tableView reloadRowsAtIndexPaths:@[deleteIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
1
votes

I now see that you want to disable delete for only certain cells. You can do this in a couple of ways:

tableView:canEditRowAtIndexPath method: Return NO where you want DELETES to be DISABLED. tableView:canMoveRowAtIndexPath: Return YES where you want to allow reordering.

You may want to think about sub-classing UITableViewCell to give it some ability to maintain its own state (so the cell knows if delete is allowed or not. Then you can interrogate the actual cell instance and determine if you should enable delete even after the list may be re-ordered.

0
votes

To hide any icon in whole table view, then in your controller just ,
override EditingStyleForRow and return UITableViewCellEditingStyle.None

  1. None: will hide any icon of left of items of table view,
  2. delete: will show remove icon
  3. Insert: will show add icon