78

Hi there I am trying to change the text that is showing in the delete button when a user swipes a uitableviewcell inside my tableview.

I have seen an example in another question thread that says to use this tableview delegate

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

My question is, how do I use this method.. I am not sure how to use this.

C.Johns
  • 10,185
  • 20
  • 102
  • 156

4 Answers4

199

In your controller managing the UITableView you should implement the UITableviewDelegate and return the title you want for your method inside the titleForDeleteConfirmationButtonForRowAtIndexPath method.

Example:

@interface CategoryAddViewController : UITableViewController
@end

@implementation CategoryAddViewController
// ...
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"Please don't delete me!";
}

@end

Leaving you off with something like that:

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Faizan S.
  • 8,634
  • 8
  • 34
  • 63
  • Great, but why doesn't the appearance of the button animate any more? After adding this, the delete button pops up instead of being animated! Edit: My bad, must have been a sim glitch. After restarting the app its ok again. – Maciej Swic Nov 11 '12 at 12:12
  • @FaizanS. I am looking into this as well. Is there no way to just change the property? Something like ... ``self.tableView.deleteButton.name = @"Remove";`` instead of overriding the method? – Scott Mar 16 '14 at 23:15
  • Not as of the time of writing. In the iOS SDK many things are achiev ed by overriding existing methods of base UI classes. – Faizan S. Mar 29 '14 at 10:08
  • 2
    the swift way: `func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:NSIndexPath) -> String{ return "Remove Me"; }` – dy_ Feb 26 '15 at 14:00
  • 1
    In this answer there is no reason to add ``. – rmaddy Dec 02 '16 at 18:12
31

In Swift it is equal, just method signature is diferent!

func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
  return "Erase"
}
Weles
  • 1,275
  • 13
  • 17
4

Just return the string that you want to display instead of delete. Say you wish to show "Erase" for all rows, the above function should contain:

return @"Erase";

Read THIS

Also in your .h file, add the UITableViewDelegate in case your view controller is not a UITableViewController already. That is it can be either:

@interface SomeView : UIViewController <UITableViewDelegate>

OR

@interface SomeView : UITableViewController
Community
  • 1
  • 1
Bourne
  • 10,094
  • 5
  • 24
  • 51
0

Swift 4.2

override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "Erase"
    }
Gopal Kohli
  • 141
  • 1
  • 7