如何防止在表格视图单元格的拖放操作中将数据追加到现有单元格?

我正在拖放表格视图单元格。但是,当它位于单元格顶部时,它会显示一个加号图标;如果我放下,该文本将被附加。如何防止出现加号图标并追加行为,而只允许重新排序?

extension ChecklistTableViewController: UITableViewDragDelegate {

    func tableView(_ tableView: UITableView,itemsForBeginning session: UIDragSession,at indexPath: IndexPath) -> [UIDragItem] {
        if let item = self.boardMan.getchecklistItem(indexPath: indexPath) {
            if let stringData = item.title.data(using: .utf8) {
                let itemProvider = NSItemProvider(item: stringData as NSData,typeIdentifier: kUTTypePlainText as String)
                let dragItem = UIDragItem(itemProvider: itemProvider)
                dragItem.localObject = (item,indexPath,tableView)
                return [dragItem]
            }
        }
        return []
    }
extension ChecklistTableViewController: UITableViewDropDelegate {

    func tableView(_ tableView: UITableView,performDropWith coordinator: UITableViewDropCoordinator) {
        if coordinator.session.hasItemsConforming(toTypeIdentifiers: [kUTTypePlainText as String]) {
            coordinator.session.loadObjects(ofClass: NSString.self) { (items) in
                switch (coordinator.items.first?.sourceIndexPath,coordinator.destinationIndexPath) {
                case (.some(let sourceIndexPath),.some(let destinationIndexPath)):
                    Log.debug("src: \(sourceIndexPath) dest: \(destinationIndexPath)")
                    let updatedIndexPaths: [IndexPath]
                    if sourceIndexPath.row < destinationIndexPath.row {
                        updatedIndexPaths =  (sourceIndexPath.row...destinationIndexPath.row).map { IndexPath(row: $0,section: 0) }
                    } else if sourceIndexPath.row > destinationIndexPath.row {
                        updatedIndexPaths =  (destinationIndexPath.row...sourceIndexPath.row).map { IndexPath(row: $0,section: 0) }
                    } else {
                        updatedIndexPaths = []
                    }
                    if let checklistItem = self.utils.getchecklistItem(indexPath: sourceIndexPath) {
                        self.tableView.beginUpdates()
                        _ = self.utils.removeChecklistItem(indexPath: sourceIndexPath)
                        _ = self.utils.insertChecklistItem(checklistItem,indexPath: destinationIndexPath)
                        self.tableView.reloadrows(at: updatedIndexPaths,with: .none)
                        self.tableView.endUpdates()
                    }
                default:
                    Log.debug("default case")
                }
            }
        }
    }

    func tableView(_ tableView: UITableView,dropSessionDidUpdate session: UIDropSession,withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
        if tableView.hasactiveDrag {
            return UITableViewDropProposal(operation: .move,intent: .insertAtDestinationIndexPath)
        }
        return UITableViewDropProposal(operation: .forbidden)
    }
}

表视图已设置了拖放委托,并添加了擦拭对象。

self.tableView.dragDelegate = self
self.tableView.dropDelegate = self
self.tableView.dragInteractionEnabled = true

请参见下面的屏幕截图。

如何防止在表格视图单元格的拖放操作中将数据追加到现有单元格?

song8531019 回答:如何防止在表格视图单元格的拖放操作中将数据追加到现有单元格?

如果您只想支持拖放操作以重新排序行,则不需要发布任何代码。

实施UITableViewDataSource和UITableViewDelegate的常规“重新排序”相关方法。设置表的dragDelegatedropDelegate。您只需从UITableViewDragDelegate和UITableViewDropDelegate中分别实现一种方法,它们的实现就很简单。

对于一个简单的单节表视图,您需要执行以下操作:

override func tableView(_ tableView: UITableView,canMoveRowAt indexPath: IndexPath) -> Bool {
    return true
}

override func tableView(_ tableView: UITableView,moveRowAt sourceIndexPath: IndexPath,to destinationIndexPath: IndexPath) {
    // Update your data model array to move the item at sourceIndexPath.row to destinationIndexPath.row
}

// MARK: - UITableViewDragDelegate

func tableView(_ tableView: UITableView,itemsForBeginning session: UIDragSession,at indexPath: IndexPath) -> [UIDragItem] {
    return []
}

// MARK: - UITableViewDropDelegate

func tableView(_ tableView: UITableView,performDropWith coordinator: UITableViewDropCoordinator) {
    // no-op
}

确保在dragDelegate或情节提要中将表视图的dropDelegateself属性设置为viewDidLoad。您还需要打开dragInteractionEnabled属性。

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dragDelegate = self
    self.tableView.dropDelegate = self
    self.tableView.dragInteractionEnabled = true
}

仅需使用拖放功能即可支持对行进行重新排序。当然,对于复杂的数据模型和多个部分,您可能还需要实现targetIndexPathForMoveFromRowAt委托方法,而在moveRowAt中的逻辑可能还需要进一步的逻辑。


有趣的是,仅iPhone才需要设置dropDelegate并启用dragInteractionEnabled。 iPad(和Mac Catalyst)版本不需要这些设置。

本文链接:https://www.f2er.com/3143159.html

大家都在问