ios 如何检测 iPhone UITableView 上的编辑模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1776045/
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
How to detect edit mode on iPhone UITableView
提问by phil swenson
For my iPhone app, I have an editable (for delete) table view. I'd like to be able to detect that the user has clicked the "Edit" button. See this image: http://grab.by/It0
对于我的 iPhone 应用程序,我有一个可编辑(用于删除)的表格视图。我希望能够检测到用户点击了“编辑”按钮。看这张图片:http: //grab.by/It0
From the docs, it looked like if I implemented :
从文档来看,如果我实现了:
- (void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
then I could detect it (although from the name of the method, I wouldn't think that). This proved not to work.
然后我可以检测到它(尽管从方法的名称来看,我不会这么认为)。这证明行不通。
Any ideas on detecting this? The reason I want to is I want to hook up a "Delete all" button in the upper left hand corner when in delete mode.
关于检测这个的任何想法?我想要的原因是我想在删除模式下在左上角连接一个“全部删除”按钮。
thanks
谢谢
回答by Stephen Darlington
It is probably not working as you expect because willBeginEditingRowAtIndexPath:
is called beforethe editing starts.
它可能没有按预期工作,因为在编辑开始之前willBeginEditingRowAtIndexPath:
被调用。
If you want to check while in another method you need the editing
property:
如果您想在另一种方法中检查,则需要该editing
属性:
@property(nonatomic, getter=isEditing) BOOL editing
If you want to do something when the 'Edit' button is pressed you need to implement the setEditing method:
如果您想在按下“编辑”按钮时执行某些操作,则需要实现 setEditing 方法:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
Which you'll find in UIViewController
. (Well, that's the most likely place; there are others.)
你会在UIViewController
. (嗯,那是最有可能的地方;还有其他地方。)
SwiftUse below code accordingly:
Swift 相应地使用以下代码:
open var isEditing: Bool // default is NO. setting is not animated.
open func setEditing(_ editing: Bool, animated: Bool)
回答by ima747
When subclassing a tableviewcontroller (what most people are going to be doing most of the time since you have to override it's delegate methods just to put data into it...) you can just override the setEditing:animated: method to grab editing state changes.
当子类化 tableviewcontroller 时(大多数人大部分时间都会这样做,因为您必须覆盖它的委托方法只是为了将数据放入其中......)您可以覆盖 setEditing:animated: 方法来获取编辑状态更改.
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
NSLog(@"Editing %i", editing);
[super setEditing:editing animated:animated];
}
That passes the state change along to the super class, but lets you jump in the middle and detect the change, or alter it if you wanted...
它将状态更改传递给超类,但允许您跳到中间并检测更改,或者根据需要更改它...
回答by Saturn
Kendall 's answer works. I did it in following way.
肯德尔的回答有效。我是按照以下方式做的。
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
NSLog(@"Can edit %d", tableView.editing);
if (tableView.editing == 1) {
[self.editButtonItem setTitle:EDIT_BUTTON_TITLE];
}else {
[self.editButtonItem setTitle:DONE_BUTTON_TITLE];
}
return YES;
}
回答by Alex Nauda
The setEditing:animated: examples didn't work for me (on iOS 6.1) to detect the state changes that occur when you enter and exit delete confirmation mode. It seems that setEditing:animated: is only called once, when the table view goes into edit mode, but not on state changes of the cells. After some debugger fun, I arrived at a method to detect the cell state change.
setEditing:animated: 示例对我(在 iOS 6.1 上)无法检测进入和退出删除确认模式时发生的状态更改。似乎 setEditing:animated: 只在表格视图进入编辑模式时调用一次,而不是在单元格的状态更改时调用。经过一些调试器的乐趣,我找到了一种检测单元状态变化的方法。
My use case is different from yours. I just wanted to hide the label when the delete button is showing so that the other cell content doesn't overlap it when the Delete button slides in. (I'm using UITableViewCellStyleValue2, the one with the blue label on the left and black label on the right.)
我的用例与你的不同。我只是想在显示删除按钮时隐藏标签,以便在删除按钮滑入时其他单元格内容不会与它重叠。 (我使用的是 UITableViewCellStyleValue2,左侧带有蓝色标签和黑色标签的标签在右边。)
(In your UITableViewCell subclass)
(在您的 UITableViewCell 子类中)
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateShowingDeleteConfirmationMask) {
// showing delete button
[self.textLabel setAlpha:0.0f]; // <-- I just wanted to hide the label
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
if (!(state & UITableViewCellStateShowingDeleteConfirmationMask)) {
// not showing delete button
[self.textLabel setAlpha:1.0f]; // <-- show the label
}
}
回答by Kendall Helmstetter Gelner
That method tells you when a user is editing a Cell, not put the table into editing mode. There is a method called when editing mode is entered, to ask each cell if it can be edited:
该方法会告诉您用户何时编辑单元格,而不是将表格置于编辑模式。进入编辑模式时调用一个方法,询问每个单元格是否可以编辑:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
I don't think overriding setEditing:animated:
makes sense, since you would have to subclass UITableView
which is extra work and a class you need for no other reason, not to mention it would have to communicate the fact that editing had been turned on back to the controller.
我不认为覆盖setEditing:animated:
是有意义的,因为你必须子类化UITableView
这是额外的工作和一个你没有其他原因需要的类,更不用说它必须将编辑已经打开的事实传达回控制器。
One other option is to simply add the Edit button yourself - it's a built in UIBarButtonSystemItem
, you can add it and then have it call your own method in which you do something specific then call setEditing:animated:
on the UITableView
itself.
另一种选择是简单地将自己添加编辑按钮-这是一个内置的UIBarButtonSystemItem
,你可以添加它,然后把它调用自己的方法,其中你做一些具体然后调用setEditing:animated:
的UITableView
本身。
The idea behind editing is that when editing is enabled, each cell is told to go to edit mode, and as asked if there are any specific editing controls that should be applied. So in theory there's no need to detect entry into editing mode beyond changing the appearance of cells. What are you trying to do when editing mode is entered?
编辑背后的想法是,当启用编辑时,每个单元格都会被告知进入编辑模式,并询问是否应该应用任何特定的编辑控件。所以理论上,除了改变单元格的外观之外,不需要检测进入编辑模式。进入编辑模式时,您要做什么?