DPM*_*itu 3 uitableview uikit ios
我知道拖放可用于在整个应用程序和应用程序之间传输数据。我对此不感兴趣。我想要的只是使用拖放功能来重新排序表视图而不传输数据。我怎么做?
小智 10
我发现这个问题的答案确实令人困惑,并且发现实施UITableViewDragDelegate
并不是这里所需要的。基本上:
UITableViewDragDelegate
,而只需实现func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
UITableViewDelegate 的方法,如下所示:func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Update the model
let mover = data.remove(at: sourceIndexPath.row)
data.insert(mover, at: destinationIndexPath.row)
}
Run Code Online (Sandbox Code Playgroud)
UITableViewDragDelegate
和 随之而来的方法,例如,func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem]{
// Handle here
}
Run Code Online (Sandbox Code Playgroud)
和
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
// Handle Drop Functionality
}
Run Code Online (Sandbox Code Playgroud)
等等。不会对此进行太多详细介绍,但您会在其他地方找到有关在不同视图和应用程序之间拖放的教程。但是,对于使用拖放对表格视图单元格进行简单的重新排序,请使用第一个选项并且不要实现UITableViewDragDelegate
.
希望这对我有帮助,因为我对这两种方法有点困惑。
如果您在本地对单个项目执行拖动,则可以使用tableView(_:moveRowAt:to:)
. 为此,您需要实现UITableViewDragDelegate。
首先设置您的代表。dragInteractionEnabled
iPhone 需要设置。
func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.dragDelegate = self
tableView.dragInteractionEnabled = true
}
Run Code Online (Sandbox Code Playgroud)
请注意,该数组正在返回单个项目。如果您返回多个项目,则将使用这些UITableViewDropDelegate
方法而不是tableView(_:moveRowAt:to:)
。您必须设置一个本地对象。
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let dragItem = UIDragItem(itemProvider: NSItemProvider())
dragItem.localObject = data[indexPath.row]
return [ dragItem ]
}
Run Code Online (Sandbox Code Playgroud)
这是移动发生的地方,实际上是UITableViewDelegate
.
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
// Update the model
let mover = data.remove(at: sourceIndexPath.row)
data.insert(mover, at: destinationIndexPath.row)
}
Run Code Online (Sandbox Code Playgroud)
如果需要,您也可以使用tableView(_:canMoveRow:at:
) tableView(_:targetIndexPathForMoveFromRowAt: toProposedIndexPath:)
。
你可以在这里阅读更多...
归档时间: |
|
查看次数: |
3915 次 |
最近记录: |