如何更改UITableView删除按钮的文本

78

你好,我想要更改用户滑动uitableviewcell时在删除按钮上显示的文本。

我在另一个问题的主题中看到了一个示例,建议使用此tableView委托方法:

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
我有一个问题,我应该如何使用这个方法?我不确定如何使用它。
4个回答

199

在管理UITableView的控制器中,您应该实现UITableviewDelegate并在titleForDeleteConfirmationButtonForRowAtIndexPath方法中返回您想要的标题。

示例:

@interface CategoryAddViewController : UITableViewController
@end

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

@end

留给你一个这样的结论: 在此输入图片描述

很好,但为什么按钮的外观不再有动画效果了?添加后,删除按钮弹出而不是被动画化!编辑:我的错,可能是模拟器故障。重新启动应用程序后,一切都正常了。 - Maciej Swic
@FaizanS。我也在研究这个问题。难道没有直接更改属性的方法吗?比如说...“self.tableView.deleteButton.name = @'Remove';”而不是覆盖方法? - Scott
截至撰写本文时还没有。在iOS SDK中,许多事情都是通过覆盖基础UI类的现有方法来实现的。 - Faizan S.
2
快捷方式:func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath:NSIndexPath) -> String{ return "删除"; } - dy_
1
在这个答案中,没有理由添加<UITableViewDelegate> - rmaddy

31

在Swift中它是相等的,只是方法签名不同!

func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String? {
  return "Erase"
}

4

只需返回您想要显示的字符串而不是删除。假设您希望为所有行显示“擦除”,则上述函数应包含:

return @"Erase";

阅读此文

而且,在您的.h文件中,添加UITableViewDelegate,以防您的视图控制器尚未是UITableViewController。这可以是:

@interface SomeView : UIViewController <UITableViewDelegate>

或者

@interface SomeView : UITableViewController

0

Swift 4.2

override func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "Erase"
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接