ios - 当 UITableView 放在 tableview 页脚上时,将单元格移动到最后一行

标签 ios swift uitableview

在 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/

相关文章:

ios - 如何防止用户共享他们的 apple id 进行应用内购买

ios - 只调用第一个请求 UIWebView

ios - UIDatePicker 崩溃应用程序

ios - 将获取数据与 UITableView 不同部分的核心数据分开

ios - dataWithContentsOfURL 通过蜂窝返回 NSCocoaErrorDomain Code=256,但不是 wifi

swift - 带有泛型参数的复杂 Swift 闭包

ios - 使用 Alamofire 从提要中提取 JSON 数据

ios - 通过 hitTest 在 Swift 中关闭键盘

ios - TableViewController 不更新单元格

ios - UITableViewController 滚动时加载我的所有单元格