Drag & Drop has been implemented so that UITableViewCell can be sorted by long-pressing. https://qiita.com/king_of_morita/items/815e78d7d285a8d1190c
However, if you move the dragged cell above another cell, a plus icon will be displayed in the upper right, and the characters in the cell will be concatenated with Drop. It's nice to have a function that allows you to concatenate cells, but this time it's unnecessary, so I investigated how to prevent cells from being concatenated.
RocketSim Recording - iPhone 12 Pro Max - 2020-12-17 22.24.20.gif
Even if you drag on the cell, the plus icon is not displayed and you cannot concatenate the cells.
Even if you drag on the cell, the plus icon is not displayed and you cannot concatenate the cells.
UITableViewDragDelegate and UITableViewDropDelegate are set in UITableView,
It is OK even if there is no implementation itself, and the sorting process is tableView (_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath)
Implement with.
HomeViewController.swift
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
//Write the sorting data update process here
}
}
//Set UITableViewDragDelegate and UITableViewDropDelegate to UITableView
func initTableView() {
tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
tableView.dropDelegate = self
}
extension HomeViewController: UITableViewDragDelegate {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
return []
}
}
extension HomeViewController: UITableViewDropDelegate {
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
//No need for contents
}
}
A search will find a way to implement Drag & Drop in Long Press, but there was a surprising blind spot. I hope you find it helpful.
https://stackoverflow.com/questions/58752989/how-to-prevent-appending-data-to-an-existing-cell-on-drag-drop-of-table-view-cel
Recommended Posts