ios UITableView 禁用滑动删除,但在编辑模式下仍然有删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/969313/
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
UITableView disable swipe to delete, but still have delete in Edit mode?
提问by willi
I want something similar as the Alarm app, where you can't swipe delete the row, but you can still delete the row in Edit mode.
我想要类似于闹钟应用程序的东西,在那里你不能滑动删除行,但你仍然可以在编辑模式下删除行。
When commented out tableView:commitEditingStyle:forRowAtIndexPath:, I disabled the swipe to delete and still had Delete button in Edit mode, but what happens when I press the Delete button. What gets called?
当注释掉 tableView:commitEditingStyle:forRowAtIndexPath: 时,我禁用了滑动以删除并且在编辑模式下仍然有删除按钮,但是当我按下删除按钮时会发生什么。叫什么?
回答by willi
Ok, it turns out to be quite easy. This is what I did to solve this:
好吧,事实证明这很容易。这就是我为解决这个问题所做的:
Objective-C
目标-C
- (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Detemine if it's in editing mode
if (self.tableView.editing)
{
return UITableViewCellEditingStyleDelete;
}
return UITableViewCellEditingStyleNone;
}
Swift 2
斯威夫特 2
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if tableView.editing {
return .Delete
}
return .None
}
Swift 3
斯威夫特 3
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if tableView.isEditing {
return .delete
}
return .none
}
You still need to implement tableView:commitEditingStyle:forRowAtIndexPath:
to commit the deletion.
您仍然需要实施tableView:commitEditingStyle:forRowAtIndexPath:
以提交删除。
回答by Marc Rochkind
Just to make things clear, swipe-to-delete will not be enabled unless tableView:commitEditingStyle:forRowAtIndexPath:
is implemented.
为了清楚起见,除非tableView:commitEditingStyle:forRowAtIndexPath:
实施,否则不会启用滑动删除。
While I was in development, I didn't implement it, and therefore swipe-to-delete wasn't enabled. Of course, in a finished app, it would always be implemented, because otherwise there would be no editing.
在我开发时,我没有实现它,因此没有启用滑动删除。当然,在一个完成的应用程序中,它总是会被实现,否则就没有编辑了。
回答by kareem
Swift Version:
迅捷版:
override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
if(do something){
return UITableViewCellEditingStyle.Delete or UITableViewCellEditingStyle.Insert
}
return UITableViewCellEditingStyle.None
}
回答by Noodybrank
You need to implement the CanEditRowAt function.
您需要实现 CanEditRowAt 函数。
You can return .delete in the EditingStyleForRowAt function so you can still delete in editing mode.
您可以在 EditingStyleForRowAt 函数中返回 .delete,这样您仍然可以在编辑模式下删除。
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
if tableView.isEditing {
return true
}
return false
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
return .delete
}
回答by Massimo Cafaro
Basically, you enable or disable editing using the methods
基本上,您可以使用以下方法启用或禁用编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
If editing is enabled, the red deletion icon appears, and a delete conformation requested to the user. If the user confirms, the delegate method
如果启用编辑,则会出现红色删除图标,并向用户请求删除确认。如果用户确认,则委托方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
is notified of the delete request. If you implement this method, then swipe to delete is automatically made active. If you do not implement this method, then swipe to delete is not active, however you are not able to actually delete the row. Therefore, to the best of my knowledge, you can not achieve what you asked for, unless using some undocumented, private APIs. Probably this is how the Apple application is implemented.
被通知删除请求。如果您实现此方法,则滑动以删除会自动激活。如果您不实现此方法,则滑动删除不会激活,但您无法实际删除该行。因此,据我所知,除非使用一些未记录的私有 API,否则您无法实现您的要求。可能这就是 Apple 应用程序的实现方式。
回答by user4272677
I came across this problem either and fixed with codes below. hope it will help you.
我也遇到了这个问题并用下面的代码修复。希望它会帮助你。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
BOOL deleteBySwipe = NO;
for (UIGestureRecognizer* g in tableView.gestureRecognizers) {
if (g.state == UIGestureRecognizerStateEnded) {
deleteBySwipe = YES;
break;
}
}
if (deleteBySwipe) {
//this gesture may cause delete unintendedly
return;
}
//do delete
}
}
}
回答by A.G
On C#:
在 C# 上:
I had the same issue where it was needed to enable/disable rows with Delete option on swipe. Multiple rows needed to be swiped left and get deleted, keep them in another colour. I achieved using this logic.
我遇到了同样的问题,需要在滑动时使用删除选项启用/禁用行。需要向左滑动多行并删除,将它们保留为另一种颜色。我使用这个逻辑实现了。
[Export("tableView:canEditRowAtIndexPath:")]
public bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{
if (deletedIndexes.Contains(indexPath.Row)){
return false;
}
else{
return true;
}
}
Note that deletedIndexes are a list of indexes which are deleted from the table without duplicates. This code check whether a row is deleted, then it disables swipe or vice versa.
请注意,deletedIndexes 是从表中删除的索引列表,没有重复项。此代码检查一行是否被删除,然后禁用滑动,反之亦然。
The equivalent delegate function is Swift is canEditRowAtIndexPath
.
等效的委托函数是 Swift is canEditRowAtIndexPath
。