iPhone - UITableView:更改默认编辑按钮名称
我的导航栏中有一个常用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
@Bojan 的方法很接近,但效率很低,因为它会为每一行调用。只需更改 viewDidLoad 开头的标题,然后执行以下操作:
@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:如果您不喜欢现有编辑按钮项的名称,请创建您自己的编辑按钮项。
在标头中声明一个 ivar editItem,然后像这样创建项目:
在 -toggleEditing 中,调用
并更新按钮的标题(以及可选的外观)以反映编辑状态。
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:
in -toggleEditing, call
and also update the title of the button (and optionally the appearance) to reflect the editing state.
这对我来说效果很好。
This works fine for me.
来补充这些答案。自定义是可行的方法但是请记住:
如果您创建自己的按钮(这将更改标题),请确保设置它的possibleTitles 属性。
否则,按钮上的过渡动画可能会变得奇怪,特别是当标题长度显着不同时。原因是,我们在点击按钮的动画时更改了按钮的标题。
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.
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.
这是我发现在使用自定义标题切换按钮时修复 editButtonItem 的临时收缩脏过渡状态的唯一方法。
即使设置 possibleTitles 也不能提供完美的结果。
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.
对于 Swift 3,您应该重写 setEditing(_ Editing: Bool,animated: Bool) UIViewController 的方法,如下所示:
此外,您应该在 viewDidLoad 上调用此方法,以在具有正确标题的视图上启动 editButtonItem,如下所示:
For Swift 3 you should override the setEditing(_ editing: Bool, animated: Bool) UIViewController's method as bellow:
Furthermore, you should call this method on viewDidLoad to the editButtonItem starts on the view with the correct title, as bellow: