用户的任务是按顺序排列单元格。(例如一个孩子)
使用以下方法移动单元格:
override func viewDidLoad() {
super.viewDidLoad()
...
table.dragDelegate = self
table.dropDelegate = self
table.dragInteractionEnabled = true
...
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
usersChooseOrder.insert(usersChooseOrder.remove(at: sourceIndexPath.row), at: destinationIndexPath.row)
}
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
print("itemsForBeginning")
return []
}
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
print(" coordinator")
}
在任何时候,您都可以通过按“完成”按钮和正确的单元格来检查放置的正确性,它们所在的位置是绿色的。告诉我在哪个方向上实现以下目标:
绿色单元格应固定到位,不再移动,无论是用手指还是由于其他单元格的移动。那些。如果将 5 移动到 4 下的位置 2,则 2 应移至位置 6,而 4 应保留在原位。如果我们将 5 移到 6 上,那么 6 和 1 会上升到 4 之上,并且 5 会出现在 4 之上。
我会很高兴任何提示。
谢谢!