gpt4 book ai didi

ios - 使用新的 iOS 11 API 重新排序 tableview 时动画不正确

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:32 26 4
gpt4 key购买 nike

我在 iOS 11 上使用新的拖放 API 对同一应用内的表格 View 中的单元格重新排序。

这是我对 UITableViewDragDelegateUITableViewDropDelegate 的实现:

extension TasksViewController: UITableViewDragDelegate {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let string = tasks[indexPath.row]
guard let data = string.data(using: .utf8) else { return [] }
let itemProvider = NSItemProvider(item: data as NSData, typeIdentifier: kUTTypePlainText as String)
let item = UIDragItem(itemProvider: itemProvider)
item.localObject = string

return [item]
}
}

extension TasksViewController: UITableViewDropDelegate {
func tableView(_ tableView: UITableView, canHandle session: UIDropSession) -> Bool {
return true
}

func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {

if session.localDragSession != nil { // Drag originated from the same app.
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}

return UITableViewDropProposal(operation: .cancel, intent: .unspecified)
}

func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
guard let destinationIndexPath = coordinator.destinationIndexPath,
let dragItem = coordinator.items.first?.dragItem,
let task = dragItem.localObject as? String,
let sourceIndexPath = coordinator.items.first?.sourceIndexPath else {
return
}

tableView.performBatchUpdates({
self.tasks.remove(at: sourceIndexPath.row)
self.tasks.insert(task, at: destinationIndexPath.row)

tableView.deleteRows(at: [sourceIndexPath], with: .none)
tableView.insertRows(at: [destinationIndexPath], with: .none)
})

coordinator.drop(dragItem, toRowAt: destinationIndexPath)
}
}

这工作正常,但有一个奇怪的故障。当最后一个单元格被拖动时,一旦它被放下,它就会出现在表格 View 的底部一瞬间然后消失。

enter image description here

我在这里错过了什么?

最佳答案

只需将表删除动画更改为.automatic,如下所示:

tableView.deleteRows(at: [sourceIndexPath], with: .automatic)

之后就不会出现那个奇怪的动画了。

关于ios - 使用新的 iOS 11 API 重新排序 tableview 时动画不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46605709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com