How to change uitableview delete button text
Asked Answered
I

4

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.

Introductory answered 12/9, 2011 at 22:56 Comment(0)
B
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

Berkey answered 12/9, 2011 at 23:4 Comment(5)
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.Antithesis
@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?Barriebarrientos
Not as of the time of writing. In the iOS SDK many things are achiev ed by overriding existing methods of base UI classes.Berkey
the swift way: func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:NSIndexPath) -> String{ return "Remove Me"; }Introduce
In this answer there is no reason to add <UITableViewDelegate>.Instantaneous
D
31

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

func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
  return "Erase"
}
Dani answered 14/10, 2015 at 0:4 Comment(0)
R
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
Runoff answered 12/9, 2011 at 23:3 Comment(0)
H
0

Swift 4.2

override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "Erase"
    }
Horseleech answered 9/8, 2020 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.