iPhone - UITableView:更改默认编辑按钮名称

发布于 2024-10-27 07:30:03 字数 474 浏览 7 评论 0原文

我的导航栏中有一个常用的 editButtonItem (由系统创建),我想更改其名称。 Si 我在 TableViewController 中写了这行:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [Some code...]

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.title = @"New name";
}

这有效,但是当进入和退出编辑模式时,其系统名称将被恢复。 例如,我尝试在 didEndEditingRowAtIndexPath 中再次强制执行此操作,但没有成功...

我应该如何修复此自定义名称,而无需自己在代码中从头开始构建按钮

I have a usual editButtonItem in my navigationBar (created by the system), and I'd like to change its name. Si I wrote this lines in my TableViewController :

- (void)viewDidLoad
{
    [super viewDidLoad];

    [Some code...]

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem.title = @"New name";
}

That works, but when entering and exiting the Edit Mode, its system name is restored.
I tried to force it again in didEndEditingRowAtIndexPath for example, but with no success...

What should I do to fix this custom name without having to build the button from start by myself in the code ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冰葑 2024-11-03 07:30:03

@Bojan 的方法很接近,但效率很低,因为它会为每一行调用。只需更改 viewDidLoad 开头的标题,然后执行以下操作:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // Make sure you call super first
    [super setEditing:editing animated:animated];

    if (editing)
    {
        self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
    }
    else
    {
        self.editButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
    }
}

@Bojan's method is close but very inefficient, since it gets called for every single row. Just change the title at the beginning on viewDidLoad and in addition do this:

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // Make sure you call super first
    [super setEditing:editing animated:animated];

    if (editing)
    {
        self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
    }
    else
    {
        self.editButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
    }
}
梦开始←不甜 2024-11-03 07:30:03

如果您不喜欢现有编辑按钮项的名称,请创建您自己的编辑按钮项。

在标头中声明一个 ivar editItem,然后像这样创建项目:

    editItem = [[UIBarButtonItem alloc] initWithTitle:@"New Name" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEditing)]

在 -toggleEditing 中,调用

[self setEditing:!self.editing animated:YES]

并更新按钮的标题(以及可选的外观)以反映编辑状态。

Create your own edit button item if you don't like the name of the existing one.

Declare an ivar editItem in your header then create the item like so:

    editItem = [[UIBarButtonItem alloc] initWithTitle:@"New Name" style:UIBarButtonItemStyleBordered target:self action:@selector(toggleEditing)]

in -toggleEditing, call

[self setEditing:!self.editing animated:YES]

and also update the title of the button (and optionally the appearance) to reflect the editing state.

残花月 2024-11-03 07:30:03
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
   if (self.tableView.editing) {
     self.editButtonItem.title =  @"CustomDoneName";
   }
   else
     self.editButtonItem.title = @"CustomEditName";
   return YES;
}

这对我来说效果很好。

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
 {
   if (self.tableView.editing) {
     self.editButtonItem.title =  @"CustomDoneName";
   }
   else
     self.editButtonItem.title = @"CustomEditName";
   return YES;
}

This works fine for me.

过潦 2024-11-03 07:30:03

来补充这些答案。自定义是可行的方法但是请记住:

如果您创建自己的按钮(这将更改标题),请确保设置它的possibleTitles 属性。

self.myEditButton.possibleTitles = [NSSet setWithObjects:@"Edit Seats", @"Done", nil];

否则,按钮上的过渡动画可能会变得奇怪,特别是当标题长度显着不同时。原因是,我们在点击按钮的动画时更改了按钮的标题。

To supplement these answers. Custom is the way to go BUT keep in mind:

If you do create your own button, which will change title, make sure you set it's possibleTitles property.

self.myEditButton.possibleTitles = [NSSet setWithObjects:@"Edit Seats", @"Done", nil];

Otherwise, the transition animation on your button may get weird, especially if the titles differ in length significantly. The reason for this is, we change the button's title as it's animating being tapped on.

毁梦 2024-11-03 07:30:03

这是我发现在使用自定义标题切换按钮时修复 editButtonItem 的临时收缩脏过渡状态的唯一方法。

即使设置 possibleTitles 也不能提供完美的结果。

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{    
    NSString *editButtonTitleBefore = self.editButtonItem.title;
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    [self setToolbarLayout];

    if (editing && ![self.editButtonItem.title isEqualToString:NSLocalizedString(@"Cancel", @"Cancel")])
    {
        self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
    }
    else if (!editing && ![editButtonTitleBefore isEqualToString:@"Edit"])
    {
        // Dirty hax to avoid the editButton system title label shrink bug
        self.editButtonItem.title = @"";
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.editButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
        });
    }
}

Here is the only way I found out to fix the temporary shrunk dirty transition state of the editButtonItem when toggling the button with custom titles.

Even setting possibleTitles does not provide a perfect result.

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{    
    NSString *editButtonTitleBefore = self.editButtonItem.title;
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
    [self setToolbarLayout];

    if (editing && ![self.editButtonItem.title isEqualToString:NSLocalizedString(@"Cancel", @"Cancel")])
    {
        self.editButtonItem.title = NSLocalizedString(@"Cancel", @"Cancel");
    }
    else if (!editing && ![editButtonTitleBefore isEqualToString:@"Edit"])
    {
        // Dirty hax to avoid the editButton system title label shrink bug
        self.editButtonItem.title = @"";
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.editButtonItem.title = NSLocalizedString(@"Edit", @"Edit");
        });
    }
}
巨坚强 2024-11-03 07:30:03

对于 Swift 3,您应该重写 setEditing(_ Editing: Bool,animated: Bool) UIViewController 的方法,如下所示:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if self.isEditing {
        self.editButtonItem.title = "[New editing title]"
    }
    else {
        self.editButtonItem.title = "[New to edit title]"
    }
}

此外,您应该在 viewDidLoad 上调用此方法,以在具有正确标题的视图上启动 editButtonItem,如下所示:

override func viewDidLoad() {
    super.viewDidLoad()

    self.setEditing(false, animated: false)
    self.navigationItem.leftBarButtonItem = self.editButtonItem
}

For Swift 3 you should override the setEditing(_ editing: Bool, animated: Bool) UIViewController's method as bellow:

override func setEditing(_ editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)

    if self.isEditing {
        self.editButtonItem.title = "[New editing title]"
    }
    else {
        self.editButtonItem.title = "[New to edit title]"
    }
}

Furthermore, you should call this method on viewDidLoad to the editButtonItem starts on the view with the correct title, as bellow:

override func viewDidLoad() {
    super.viewDidLoad()

    self.setEditing(false, animated: false)
    self.navigationItem.leftBarButtonItem = self.editButtonItem
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文