Tableview Drag and Drop

I have two table views in a single view controller and want to be able to drag and drop between them. I have been trying to implement a tutorial I found online with no success. Below is the code if you have any input. Thank you in advance.

estimateList is a NSObject with a takeOffList array which is the datasource for the takeOffTableView
warehouseList is a NSObject which is the datasource for the warehouseTableView

@IBOutlet weak var takeOffTableView: UITableView!
@IBOutlet weak var warehouseTableView: UITableView!

func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let string = tableView == takeOffTableView ? estimateList.takeOffList[indexPath.row] : dataModel.warehouseLists[indexPath.row]
        guard let data = .utf8) else { return [] }
        let itemProvider = NSItemProvider(item: data as NSData, typeIdentifier: kUTTypePlainText as String)
        return [UIDragItem(itemProvider: itemProvider)]
    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
        let destinationIndexPath: IndexPath
        if let indexPath = coordinator.destinationIndexPath {
            destinationIndexPath = indexPath
        } else {
            let section = tableView.numberOfSections - 1
            let row = tableView.numberOfRows(inSection: section)
            destinationIndexPath = IndexPath(row: row, section: section)
        // attempt to load strings from the drop coordinator
        coordinator.session.loadObjects(ofClass: NSString.self) { items in
            // convert the item provider array to a string array or bail out
            guard let strings = items ? as? [String] else { return }
            // create an empty array to track rows we've copied
            var indexPaths = [IndexPath]()
            // loop over all the strings we received
            for (index, string) in strings.enumerated() {
                // create an index path for this new row, moving it down depending on how many we've already inserted
                let indexPath = IndexPath(row: destinationIndexPath.row + index, section: destinationIndexPath.section)
                // insert the copy into the correct array
                if tableView == self.takeOffTableView {
                    self.takeOffReference.insert(string, at: indexPath.row)
                } else {
                    self.dataModel.warehouseLists.insert(string, at: indexPath.row)
                // keep track of this new row
            // insert them all into the table view at once
            tableView.insertRows(at: indexPaths, with: .automatic)

Perhaps @bdmoakley could help? This is regarding the challenge that appears under the Table Views lesson for the iOS path.

I built a prototype of the drag and drop with the tableviews, but I never made a course on it. The reason is that it required a lot of code that pretty much duplicated our screencast on dragging and dropping. The best place is to check over here:

The ios version is different but it should work with a few tweaks.

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.