【发布时间】:2019-11-07 16:22:31
【问题描述】:
我正在拖放一个表格视图单元格。但它在单元格顶部显示一个加号图标,如果我放下,文本会被附加。如何防止这个加号图标和附加行为,只允许重新排序?
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
请看下面的截图。
【问题讨论】:
标签: ios swift uitableview drag-and-drop