我有一个带有单元格的 UITableView,我希望用户能够通过使用每个单元格右侧的“ handle ”上下拖动它们来重新排序。我不想实现“拖放”功能,因为这些单元格后面的数据在这个表之外没有任何意义(考虑到表 View 及其单元格后面的数据,实现 NSItemProvider 看起来是一个丑陋的过程)。到目前为止,一切都已实现并运行良好。看起来有点像this article的结果.
下一步是,我想知道用户何时“抬起”或开始移动单元格——类似于 dragStateDidChange当一个是使用拖放时的能力。我什至想在单元格移出其位置之前收到此通知——因为 UI 将单元格背景变成白色并将其“抬高”到表格上方。
如何在不实现“拖放”的情况下获得此通知?
我尝试过的事情没有完成我想要的:
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath?
在 UITableViewDelegate 代码中;当用户触摸拖动 handle 时不会调用它。 func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath
当拖动的单元格改变位置时触发,但在此之前不会触发;它没有捕捉到我正在寻找的提升/拖动过程的开始。 我怀疑必须有一个简单的通知,即单元格已被提起并正在使用 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/