启用/禁用滑动以动态删除UITableViewCell上的功能

时间:2013-08-22 13:43:36

标签: ios ipad ios6

我在UITableViewCell上添加了一个手势识别器,将单元格拖动到同一个视图控制器中的其他视图。为此,我在单元格中添加了长按手势识别器。

但是当我们不拖动单元格时,我还需要滑动来删除功能。但是当我开始拖动并向一个方向(向右或向左)移动时,Cell也会触发刷卡到删除(默认)的调用。

此刻我需要停止此调用。重新加载tableView不是一个选项,因为重新加载后我会松开所选的单元格以进行拖动。

2 个答案:

答案 0 :(得分:2)

尝试下面的代码,它是ios中可用的UITableViewDelegate方法之一

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

return UITableViewCellEditingStyleNone;
}

您还可以使用此方法中提供的 indexPath 关闭特定部分中特定行的滑动以删除

Swift 3版本:

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

答案 1 :(得分:0)

如果其中任何一个对您有用,可以猜测一下 -

  1. 检查tableView:shouldIndentWhileEditingRowAtIndexPath:委托方法。
  2. 关闭编辑模式直接滑动手势和长按手势以及处理多个手势的功能(只是为了精确)。
  3. 现在,你可以做的是增加长按时间或只添加功能来检测多个手势。

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    { 
        return YES; 
    }
    
相关问题