关于TableView代码正确可是无法实现移动行效果的问题

TableView代码无误,可无法实现效果。

@IBAction func EditButton(_ sender: Any) {
       
        ListTableView.isEditing = !ListTableView.isEditing
        switch ListTableView.isEditing{
        case true:
            EditButton.title = "done"
        case false:
            EditButton.title = "edit"
            
        }
    }
    ···
    //移动行
    //是否可编辑
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
     // 编辑模式
    override func setEditing(_ editing: Bool, animated: Bool) {
        super.setEditing(editing, animated: animated)
        ListTableView.setEditing(editing, animated: true)
    
    //允许重新排序单元格,如果返回否则无法重新排序
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    //执行行重排的操作
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath){
        
        let item = Array[sourceIndexPath.row]
        
        Array.remove(at: sourceIndexPath.row)
        Array.insert(item, at: destinationIndexPath.row)
    }
    
    override func didReceiveMemoryWarning() {
    }

}

这个是此方法的参考链接:
How To Reorder/Rearrange Table View Cells In Xcode 8 (Swift 3)

同时也参考了道长的swift30个项目里的todo里的方法,参考链接:
soapyigu/Swift-30-Projects/Project 04 - Todo/Todo.xcodeproj/

可依然无法实现效果。

这两个方法在我的GitHub里,欢迎评论补充。
FelixXiong/Swift-Function-code-library-example/TableView相关.md

关于此问题之前的问题:
UITableView的移动行的问题
可是并未解决效果。

阅读 3.1k
1 个回答

设置UITableView的editingtrue

tableView.setEditing(true, animated: true)

然后重写sourceIndexPath方法

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath){
        
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题