ios - 重新排序 UITableView : notification when user first starts dragging with the handles

标签 ios swift uitableview

我有一个带有单元格的 UITableView,我希望用户能够通过使用每个单元格右侧的“ handle ”上下拖动它们来重新排序。我不想实现“拖放”功能,因为这些单元格后面的数据在这个表之外没有任何意义(考虑到表 View 及其单元格后面的数据,实现 NSItemProvider 看起来是一个丑陋的过程)。到目前为止,一切都已实现并运行良好。看起来有点像this article的结果.

下一步是,我想知道用户何时“抬起”或开始移动单元格——类似于 dragStateDidChange当一个是使用拖放时的能力。我什至想在单元格移出其位置之前收到此通知——因为 UI 将单元格背景变成白色并将其“抬高”到表格上方。

如何在不实现“拖放”的情况下获得此通知?

我尝试过的事情没有完成我想要的:

  • 识别长按手势:我确实在这些单元格上使用手势识别器进行点击等;似乎当用户触摸拖动 handle 以重新排列单元格时,此操作不会触发长按手势。试过了,没有成功。
  • Will-select-row-at:我实现了功能func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?在 UITableViewDelegate 代码中;当用户触摸拖动 handle 时不会调用它。
  • targetIndexPathForMoveFromRowAt:函数 func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath当拖动的单元格改变位置时触发,但在此之前不会触发;它没有捕捉到我正在寻找的提升/拖动过程的开始。
  • 同样,也不会调用 willBeginEditingRowAt 和 shouldHighlightRowAt。
  • 各种 scrollView 函数:它们都在用户滚动整个表格时调用,而不是在用户重新排序时调用。
  • 注意“设置编辑”:拖动 handle 需要始终存在,因此“编辑”始终设置为 true。
  • Somewhat similar question ,不幸的是没有我可以使用的答案(而且也不是在 Swift 中)。

  • 我怀疑必须有一个简单的通知,即单元格已被提起并正在使用 handle 拖动,即使它尚未移动到足够远以高于或低于其旧位置。毕竟,操作系统“知道”这一点,因为它会改变背景并添加阴影等等。任何访问该通知以便其他代码也可以对其做出响应的帮助将不胜感激!

    最佳答案

    有点奇怪,这些没有记录,但你可以在你的 UITableViewController 中定义以下函数(或在 UITableViewDelegate 中)将分别在单元格拖动开始或结束时调用:

    @objc func tableView(_ tableView: UITableView, willBeginReorderingRowAtIndexPath indexPath: IndexPath) {
        // Dragging started
    }
    
    @objc func tableView(_ tableView: UITableView, didEndReorderingRowAtIndexPath indexPath: IndexPath) {
        // Dragging ended
    }
    
    

    关于ios - 重新排序 UITableView : notification when user first starts dragging with the handles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53583563/

    相关文章:

    ios - NSURLSession 认证

    swift - 在 willDisplay 中配置自调整大小的单元格

    ios - 我怎么知道 Tab Bar Controller 索引是否以编程方式快速更改?

    ios - UITapGestureRecognizer 与 didSelectRowAtIndexPath 并发性

    ios - 如何在 UITableView 之上显示 UIView?

    ios - 在UITableView中使用自动布局以获取动态单元格布局和可变的行高

    ios - 为什么 "willActivate"中的方法 "WKInterfaceController"已经为下一页调用了?

    iphone - Settings.bundle ... 为什么我的代码不能通过 [NSUserDefaults standardUserDefaults] 访问这些值?

    ios - 准备 segue : Cannot convert value of type 'UIViewController' to specified type 'SecondViewController'

    ios - FIRAnalyticsConnector : building for Mac Catalyst,,但链接到为 iOS 模拟器构建的目标文件中