UITableView长按拖动排序时出现禁止图标问题求助
UITableView长按排序时禁止图标异常及单元格消失问题
我在UITableView中实现长按手势重排序单元格时遇到两个问题:
- 长按并轻微拖动单元格时,单元格右上角会出现灰色禁止图标,该图标会一直存在,直到调用代理方法
tableView(_:dropSessionDidUpdate:withDestinationIndexPath:)后才消失;若将单元格放回原位置,图标不会再出现。但如果完全移除类声明中的UITableViewDropDelegate协议,放回原位置时禁止图标会再次出现——这不符合预期,因为放回原位置并非禁止操作。我曾查找过类似问题,但未得到有效解答。 - 排序后有时单元格会消失,直到滚出屏幕(推测触发了
tableView(_:cellForRowAt:)方法)才重新显示,我有一些思路但认为这是不该出现的异常,希望得到相关帮助。
相关代码
// 在viewDidLoad中,类声明已遵循UITableViewDragDelegate和UITableViewDropDelegate协议 tableView.dragInteractionEnabled = true tableView.dragDelegate = self tableView.dropDelegate = self // 返回空数组是为了让tableView优先调用tableView(_:canMoveRowAt:),从而只允许指定indexPath的单元格可移动 func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] { // 原代码 //let dragItem = UIDragItem(itemProvider: NSItemProvider()) //dragItem.localObject = myData[indexPath.row] //return [dragItem] // 新代码 return [] } func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal { if session.localDragSession != nil, tableView.hasActiveDrag, session.items.count == 1 { return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath) } return UITableViewDropProposal(operation: .cancel) // 测试中从未被调用,符合预期 } // 该方法从未被调用,因此已移除 // func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) { // } func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // 自定义逻辑 } func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath { // 自定义逻辑 } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { // 自定义逻辑 }
内容的提问来源于stack exchange,提问作者Merricat
相关产品推荐
相关产品推荐

