我正在拖放表格视图单元格。但是,当它位于单元格顶部时,它会显示一个加号图标;如果我放下,该文本将被附加。如何防止出现加号图标并追加行为,而只允许重新排序?
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
请参见下面的屏幕截图。