代码之家  ›  专栏  ›  技术社区  ›  willi

UITableView禁用滑动删除,但在编辑模式下仍有删除功能?

  •  98
  • willi  · 技术社区  · 17 年前

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

    7 回复  |  直到 14 年前
        1
  •  290
  •   phatmann Trevor    9 年前

    好吧,事实证明这很容易。这就是我为解决这个问题所做的:

    内存管理

    - (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:commitEditingStyle:forRowAtIndexPath: 提交删除。

        2
  •  10
  •   Marc Rochkind    14 年前

    为了清楚起见,除非以下情况,否则不会启用滑动删除 tableView:commitEditingStyle:forRowAtIndexPath: 实施。

        3
  •  5
  •   Noodybrank    7 年前

    您可以在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
    }
    
        4
  •  4
  •   kareem    11 年前

    Swift版本:

    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
    
        if(do something){
    
            return UITableViewCellEditingStyle.Delete or UITableViewCellEditingStyle.Insert
    
        }
        return UITableViewCellEditingStyle.None
    
    }
    
        5
  •  2
  •   Massimo Cafaro    17 年前

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

    - (void)setEditing:(BOOL)editing animated:(BOOL)animated
    

    如果启用了编辑,则会出现红色删除图标,并向用户请求删除确认。如果用户确认,则委托方法

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

    收到删除请求的通知。如果您采用此方法,则滑动删除会自动激活。如果不实现此方法,则滑动删除功能不起作用,但您无法实际删除该行。因此,据我所知,除非使用一些未记录的私有API,否则你无法实现你的要求。也许这就是苹果应用程序的实现方式。

        6
  •  0
  •   Alvin George    8 年前

    关于C#:

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

    [Export("tableView:canEditRowAtIndexPath:")]
    public bool CanEditRow(UITableView tableView, NSIndexPath indexPath)
    {
    
        if (deletedIndexes.Contains(indexPath.Row)){
            return false;
        }
        else{
            return true;
        }
    
    }
    

    请注意,deletedIndexes是从表中删除的索引列表,没有重复项。此代码检查是否删除了行,然后禁用滑动,反之亦然。

    Swift的等效委托函数是 canEditRowAtIndexPath .

        7
  •  0
  •   user4272677    7 年前

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

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

    }