【问题标题】:Enable/Disable the swipe to delete functionality on UITableViewCell dynamically启用/禁用滑动以动态删除 UITableViewCell 上的功能
【发布时间】:2013-08-25 06:55:09
【问题描述】:

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

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

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

【问题讨论】:

  • 试过了....但是每次重新加载表格时都会调用此函数....并且如前所述,我无法重新加载表格。此外,editingstyle 属性是单元格的只读属性。 .....

标签: ios ipad ios6


【解决方案1】:

尝试下面的代码,它是 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
}

【讨论】:

    【解决方案2】:

    一些猜测,如果它们中的任何一个对你有用 -

    1. 检查tableView:shouldIndentWhileEditingRowAtIndexPath: 委托方法。
    2. 关闭编辑模式,直接添加滑动手势和长按手势以及处理多个手势的功能(只是为了准确)。

    现在,您可以在这里做的是增加长按时间或只是添加检测多个手势的功能。

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    { 
        return YES; 
    }
    

    【讨论】:

    • 您是否建议使用自定义滑动删除手势?
    • 现在,你可以在这里做的是增加长按时间或者只是添加检测多个手势的功能。 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; }
    • @anky_beliveMe --- 谢谢伙计......它工作得很好.....只需要{return No;}才能实现我的功能
    • 只需编辑您在评论中指定的此功能的答案。那时会很乐意这样做....
    • 完成。我做出了改变。!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-22
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多