在 iOS 11 中,我们有了新的拖放 API,它使用现有的 UITableViewDelegate 方法对同一表格 View 中的单行重新排序。我使用空 View 作为 tableView.tableFooterView ,以便隐藏最后一个实际单元格下方的单元格分隔符。但是,如果只有几个单元格并且显示页脚,则在尝试将单元格移动到最终位置时可能会很尴尬,因为表格 View 页脚不接受拖放。
有什么方法可以使用
func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath
或 UITableViewDropDelegate
中的另一种方法将 tableview 页脚重定向到最后一个 IndexPath
?
我能想到的唯一其他选项是使页脚成为自定义放置目标,但这会调整放置预览的大小并更改处理放置的代码路径。
最佳答案
Swift 5 iOS 13.5
我最终做到了这一点。
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
//remove animation when no different destinationIndexPath is there, meaning we moved over tableview area with no cells
if sourceIndexPath == destinationIndexPath{
UIView.setAnimationsEnabled(false)
}
else{
UIView.setAnimationsEnabled(true)
}
myarray.remove(at: sourceIndexPath.row)
if sourceIndexPath != destinationIndexPath {
myarray.insert(todo, at: destinationIndexPath.row)
}
else if sourceIndexPath == destinationIndexPath{
let therow = myarray.count
myarray.insert(todo, at: therow)
//just to add a litle delay before row appears at the end. othewwise looks ugly.
DispatchQueue.main.asyncAfter(deadline: .now()+0.1) {
self.topTableView.reloadData()
}
}
}
关于ios - 当 UITableView 放在 tableview 页脚上时,将单元格移动到最后一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47915064/