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

时间:2017-12-20 22:08:47

标签: ios swift uitableview

使用iOS 11,我们有了新的拖放API,它使用现有的UITableViewDelegate方法在同一个tableview中重新排序单行。我使用空视图作为tableView.tableFooterView,以隐藏最后一个实际单元格下方的单元格分隔符。但是,如果只显示了几个单元格并且显示了页脚,则在尝试将单元格移动到最终位置时,由于tableview页脚不接受掉落,因此可能会很尴尬。

有没有办法使用

func tableView(_ tableView: UITableView, targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath, toProposedIndexPath proposedDestinationIndexPath: IndexPath) -> IndexPath

UITableViewDropDelegate中的另一种方法,用于将dropview从tableview页脚重定向到最后一个IndexPath

我能想到的唯一其他选项是使页脚成为自定义放置目标,但这会调整放置预览的大小并更改处理放置的代码路径。

2 个答案:

答案 0 :(得分:0)

我最终将表格视图页脚作为自定义放置目标。事实证明,拖动的单元格在离开完整表格视图的边界之前不会调整大小,因此这是一个非常流畅的解决方案。唯一显着的区别是,使用这种方法,单元格将从其原始索引动画而不是从最后一个拖动位置下降。

答案 1 :(得分:0)

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()
        }
    }
    
}