xcode iPhone Storyboard 编辑一个表格视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7921579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone Storyboard Editing a table view
提问by markplee
I've been trying to learn the new Storyboard feature in Xcode and I've run into a problem with trying to set a UITableView to edit mode.
我一直在尝试学习 Xcode 中的新 Storyboard 功能,但在尝试将 UITableView 设置为编辑模式时遇到了问题。
So far my storyboard looks like this:
到目前为止,我的故事板看起来像这样:
NavigationController -> UIViewController (subclass with tableview property)
NavigationController -> UIViewController(具有 tableview 属性的子类)
I added a Navigation Item and a Bar Button item to the view controller scene, so I do see an edit button. It didn't do anything automagically, so I tried linking it's selector to the setEditing method of the tableview delegate. This did put it into editing mode. However, the edit button did not change to a "Done" button and so there is no way to get out of editing mode.
我在视图控制器场景中添加了一个导航项和一个栏按钮项,所以我确实看到了一个编辑按钮。它没有自动执行任何操作,所以我尝试将它的选择器链接到 tableview 委托的 setEditing 方法。这确实将其置于编辑模式。但是,编辑按钮并未更改为“完成”按钮,因此无法退出编辑模式。
Do I have to create another Navigation item for the Done button? How do I connect it so that it appears at the right time and works correctly?
我是否必须为“完成”按钮创建另一个导航项目?如何连接它以使其在正确的时间出现并正常工作?
回答by yassassin
I think that also with Storyboard, the only way (for sure, the easiest one) to implement a working edit/done button, is to use the following code:
我认为,对于 Storyboard,实现工作编辑/完成按钮的唯一方法(当然,最简单的方法)是使用以下代码:
- (void)viewDidLoad
{
[super viewDidLoad];
...
//set the edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
This is the solution that Apple itself implements if you select a "Master-Detail Application" template for your project.
如果您为项目选择“主从应用程序”模板,则这是 Apple 自己实现的解决方案。
Probably Storyboard is still not perfect, and hopefully it will be improved from Apple in next releases...
可能 Storyboard 仍然不完美,希望 Apple 在下一个版本中对其进行改进......
回答by Graham
I just started using Storyboards, so I also wanted to use the Storyboard to add my Edit button. It is annoying to have taken the time to learn how to use a new tool but find you need a roll of duct tape to patch up the holes.
我刚开始使用 Storyboards,所以我也想使用 Storyboard 添加我的编辑按钮。花时间学习如何使用新工具但发现您需要一卷胶带来修补孔洞,这很烦人。
You can get it to work, but need to add a Custom button. In the Attributes inspector make sure the Identifier is Custom and the title is Edit.
你可以让它工作,但需要添加一个自定义按钮。在属性检查器中,确保标识符为自定义,标题为编辑。
Then add something like this in your .m
然后在你的 .m 中添加这样的东西
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = @"Edit";
[super setEditing:NO animated:YES];
} else {
sender.title = @"Done";
[super setEditing:YES animated:YES];
}
}
Have your Custom Edit button call the setEditMode method.
让您的自定义编辑按钮调用 setEditMode 方法。
Can only hope they will fix the implementation of the Edit button in the Storyboard editor in the future.
只能希望他们将来修复 Storyboard 编辑器中 Edit 按钮的实现。
回答by David Bieder
To summarize:
总结一下:
The Button, returned by UIViewController.editButtonItem
is a special toggling button with special behavior that calls - (void)setEditing:(BOOL)editing animated:(BOOL)animated
if pressed.
返回的 ButtonUIViewController.editButtonItem
是一个特殊的切换按钮,具有特殊行为,- (void)setEditing:(BOOL)editing animated:(BOOL)animated
按下时调用。
The Button, returned by UINavigationController.editButtonItem
is a simple Button, just labeled with "Edit".
返回的 ButtonUINavigationController.editButtonItem
是一个简单的 Button,只是标有“Edit”。
The Storyboard allows to select the latter one.
故事板允许选择后者。
回答by justin
If you are using the navigation controller to push to the view controller, simply set self.navigationItem.rightBarButtonItem = self.editButtonItem;
, which will put the default Edit button in the right. If the navigation bar is not visible, call self.navigationController.navigationBarHidden = NO;
. Those would be called in the viewDidLoad
method, or something similar. Then in order to get the tableView to respond to the edit call, use the following method:
如果您使用导航控制器推送到视图控制器,只需设置self.navigationItem.rightBarButtonItem = self.editButtonItem;
,这会将默认的编辑按钮放在右侧。如果导航栏不可见,请调用self.navigationController.navigationBarHidden = NO;
。这些将在viewDidLoad
方法或类似的东西中被调用。然后为了让tableView响应edit调用,使用如下方法:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
}
That should do what you want it to do. If you have any issues, just say so and we can narrow down the details
那应该做你想让它做的事情。如果您有任何问题,只需说出来,我们就可以缩小细节范围
回答by charleyh
To add to @Graham answer, you might also want to change the style so you can have the "Done" button style (the blue color). Something like this:
要添加到@Graham 答案中,您可能还想更改样式,以便拥有“完成”按钮样式(蓝色)。像这样的东西:
- (IBAction)setEditMode:(UIBarButtonItem *)sender {
if (self.editing) {
sender.title = @"Edit";
sender.style = UIBarButtonItemStylePlain;
[super setEditing:NO animated:YES];
} else {
sender.title = @"Done";
sender.style = UIBarButtonItemStyleDone;
[super setEditing:YES animated:YES];
}
}
回答by angelos.p
In case that you have UIViewController and inside this you added a UITableVIew. If you want to add an edit UIBarButton in order to interact with UITableView, try:
如果您有 UIViewController 并且在其中添加了一个 UITableVIew。如果你想添加一个编辑 UIBarButton 以便与 UITableView 交互,请尝试:
Add this line...
添加这一行...
- (void)viewDidLoad
{
[super viewDidLoad];
...
self.navigationItem.leftBarButtonItem = self.editButtonItem;
...
}
and this method
而这个方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.myListTableView setEditing:editing animated:animated];
if(self.myListTableView.editing) {
NSLog(@"editMode on");
} else {
NSLog(@"editMode off");
}
}
where
在哪里
@property (weak, nonatomic) IBOutlet UITableView *myListTableView;
回答by kitschmaster
one can use the dumb, not working Edit button from the Storyboard editor and then programmatically replace it with the UIViewController.editButtonItem
.
可以使用 Storyboard 编辑器中愚蠢的、不起作用的 Edit 按钮,然后以编程方式将其替换为UIViewController.editButtonItem
.
in viewDidLoad:
在 viewDidLoad 中:
NSMutableArray *toolbarItems = [NSMutableArray arrayWithArray:self.toolbarItems];
[toolbarItems replaceObjectAtIndex:0 withObject:self.editButtonItem];
[self setToolbarItems:toolbarItems];
this code assumes one has added the dumb Edit button as the leftmost item on the toolbar in the Storyboard.
这段代码假设已经添加了愚蠢的编辑按钮作为故事板工具栏上最左边的项目。