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

发布于 2022-09-06 13:10:35 字数 2003 浏览 18 评论 0

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的移动行的问题
可是并未解决效果。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满天都是小星星 2022-09-13 13:10:35

设置UITableView的editingtrue

tableView.setEditing(true, animated: true)

然后重写sourceIndexPath方法

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath){
        
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文