Ios UITableView是否禁用刷卡删除,但在编辑模式下仍有删除?

Ios UITableView是否禁用刷卡删除,但在编辑模式下仍有删除?,ios,cocoa-touch,uitableview,uikit,Ios,Cocoa Touch,Uitableview,Uikit,我想要类似于报警应用程序的东西,你不能滑动删除行,但你仍然可以在编辑模式下删除行 当注释掉tableView:CommittedItingStyle:forRowAtIndexPath:,我禁用了要删除的滑动,并且在编辑模式下仍有删除按钮,但当我按下删除按钮时会发生什么情况。调用什么?基本上,您可以使用这些方法启用或禁用编辑 - (void)setEditing:(BOOL)editing animated:(BOOL)animated 如果启用编辑,则会显示红色删除图标,并向用户请求删除确认

我想要类似于报警应用程序的东西,你不能滑动删除行,但你仍然可以在编辑模式下删除行


当注释掉tableView:CommittedItingStyle:forRowAtIndexPath:,我禁用了要删除的滑动,并且在编辑模式下仍有删除按钮,但当我按下删除按钮时会发生什么情况。调用什么?

基本上,您可以使用这些方法启用或禁用编辑

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
如果启用编辑,则会显示红色删除图标,并向用户请求删除确认。如果用户确认,则委托方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

收到删除请求的通知。如果实现此方法,则“滑动到删除”将自动激活。如果未实现此方法,则“滑动到删除”处于非活动状态,但您无法实际删除该行。因此,据我所知,除非使用一些未记录的私有API,否则无法实现您的要求。可能这就是苹果应用程序的实现方式。

好的,结果很简单。这就是我为解决这个问题所做的:

目标-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

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    if tableView.editing {
         return .Delete
    }

    return .None
}
Swift 3

override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    if tableView.isEditing {
        return .delete
    }

    return .none
}

您仍然需要实现
tableView:committeeditingstyle:forRowAtIndexPath:
来提交删除操作。

为了清楚起见,除非实现了
tableView:committeeditingstyle:forRowAtIndexPath:
,否则将不会启用滑动删除操作

在开发过程中,我没有实现它,因此没有启用“刷到删除”。当然,在一个完成的应用程序中,它将始终被实现,因为否则将无法进行编辑。

Swift版本:

override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {

    if(do something){

        return UITableViewCellEditingStyle.Delete or UITableViewCellEditingStyle.Insert

    }
    return UITableViewCellEditingStyle.None

}
关于C:

我也遇到了同样的问题,即在滑动时需要使用Delete选项启用/禁用行。多行需要向左滑动并删除,请将它们保留为另一种颜色。我用这个逻辑实现了

[Export("tableView:canEditRowAtIndexPath:")]
public bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
{

    if (deletedIndexes.Contains(indexPath.Row)){
        return false;
    }
    else{
        return true;
    }

}
请注意,DeletedIndex是从表中删除的索引列表,没有重复项。此代码检查行是否已删除,然后禁用刷卡,反之亦然


等效的委托函数是Swift,即
canEditRowAtIndexPath

您需要实现CanEditRowAt函数

您可以在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
}

我也遇到了这个问题,并用下面的代码进行了修复。希望它能帮助你

- (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
}

}

我通过返回UITableViewCellEditingStyleDelete在tableView:editingStyleForRowAtIndexPath:中解决了这个问题。如果它处于编辑模式,则再次自动启用“滑动到删除”。是否?否,如果未处于编辑模式,则不会启用“滑动删除”。这就是我返回UITableViewCellEditingStyleNone作为默认值的原因。忘了提到在CommittedItingStyle中需要if(editingStyle==UITableViewCellEditingStyleDelete):好的,使用此语句可以执行删除操作,但它具有不同的可视性。有没有可能有和刷卡版一样的视觉效果呢?@giuseppe没关系。他回指自我并没有错。该委托方法引用的是同一个tableView,因此他可以使用或。