[Swift] You can concatenate cells with Drag & Drop of UITableView

Introduction

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

After modification so that cells cannot be connected

Even if you drag on the cell, the plus icon is not displayed and you cannot concatenate the cells.

After modification so that cells cannot be connected

Even if you drag on the cell, the plus icon is not displayed and you cannot concatenate the cells.

Correction code

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
    }
}

Summary

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.

Reference site

https://stackoverflow.com/questions/58752989/how-to-prevent-appending-data-to-an-existing-cell-on-drag-drop-of-table-view-cel

Recommended Posts

[Swift] You can concatenate cells with Drag & Drop of UITableView
[Rails] Implementation of drag and drop function (with effect)
Drag and drop files with JavaFX
[Swift 5] Implementation of membership registration with Firebase
The basic basis of Swift custom cells
When is it said that you can use try with a Swift error?