【问题标题】:UITableView disable swipe to delete, but still have delete in Edit mode?UITableView 禁用滑动删除,但在编辑模式下仍有删除?
【发布时间】:2010-11-01 10:38:24
【问题描述】:

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

当注释掉 tableView:commitEditingStyle:forRowAtIndexPath: 时,我禁用了滑动删除并且在编辑模式下仍然有删除按钮,但是当我按下删除按钮时会发生什么。叫什么?

【问题讨论】:

    标签: ios cocoa-touch uitableview uikit


    【解决方案1】:

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

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

    }

    【讨论】:

      【解决方案2】:

      您需要实现 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
      }
      

      【讨论】:

        【解决方案3】:

        在 C# 上:

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

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

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

        等效的委托函数是 Swift 是canEditRowAtIndexPath

        【讨论】:

          【解决方案4】:

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

          Objective-C

          - (UITableViewCellEditingStyle)tableView:(UITableView *)aTableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
          {
              // Detemine if it's in editing mode
              if (self.tableView.editing)
              {
                  return UITableViewCellEditingStyleDelete;
              }
          
              return UITableViewCellEditingStyleNone;
          }
          

          斯威夫特 2

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

          斯威夫特 3

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

          你仍然需要实现tableView:commitEditingStyle:forRowAtIndexPath:来提交删除。

          【讨论】:

          • bu 然后滑动删除自动启用,再次。还是不行?
          • 不,滑动删除不会启用,如果它不处于编辑模式。这就是我默认返回 UITableViewCellEditingStyleNone 的原因。
          • 忘了在 commitEditingStyle 中提到你需要 if (editingStyle == UITableViewCellEditingStyleDelete):
          • 好的,有了这个语句,你得到了删除动作,但它有不同的视觉效果。是否有机会以与滑动版本相同的视觉效果进行该操作?
          • @giuseppe 没关系。他提到自我并没有错。该委托方法指的是同一个 tableView,所以他可以使用 or。
          【解决方案5】:

          Swift 版本:

          override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
          
              if(do something){
          
                  return UITableViewCellEditingStyle.Delete or UITableViewCellEditingStyle.Insert
          
              }
              return UITableViewCellEditingStyle.None
          
          }
          

          【讨论】:

            【解决方案6】:

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

            在开发过程中,我没有实现它,因此没有启用滑动删除。当然,在一个完成的应用程序中,它总是会被实现,否则就没有编辑。

            【讨论】:

              【解决方案7】:

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

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

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

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

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

              【讨论】:

              • 我通过在 tableView 中返回 UITableViewCellEditingStyleDelete 解决了这个问题:editingStyleForRowAtIndexPath: if it's in editing mode.
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多