有的时候还是想复用iOS自身的设计逻辑,减少代码编写
本次主要是想实现对于列表的排序,编辑修改删除操作
涉及到的操作方式如下

  1. 向左滑动出现编辑删除选项
  2. 长按列表项目排序;

效果图如下
请添加图片描述

先把页面画起来

import UIKit

class ViewController: UIViewController {

    var datas: [String] = []
    var tableview: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableview = UITableView.init(frame: .init(x: 0, y: 0, width: view.frame.width, height: view.frame.height), style: .insetGrouped)
        tableview.delegate = self
        tableview.dataSource = self
        
        tableview.register(UITableViewCell.self, forCellReuseIdentifier: "ViewController")
        
        self.view.addSubview(tableview)
        
        self.datas = [
            "床前明月光",
            "疑是地上霜",
            "举头望明月",
            "低头思故乡",
        ]
    }
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier: "ViewController", for: indexPath)
        var config = cell.defaultContentConfiguration()
        config.text = datas[indexPath.row]
        cell.contentConfiguration = config
        return cell
    }
}

首先来实现滑动出现编辑删除选项

感觉不需要增加tableview编辑模式,不过也可以进入编辑模式修改。

extension ViewController {
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .destructive, title: "删除") { (action, view, handler) in
            print("delete")
            self.datas.remove(at: indexPath.row)
            self.tableview.reloadData()
        }
        let editAction = UIContextualAction(style: .normal, title: "编辑") { action, view, handler in
            print("edit")
            
        }
        deleteAction.backgroundColor = .red
        let configuration = UISwipeActionsConfiguration(actions: [deleteAction, editAction])
        configuration.performsFirstActionWithFullSwipe = false
        return configuration
    }
}

编辑模式切换如下:根据需要使用一下。

tableview.setEditing(true, animated: true)

长按列表项目排序

开启配置

// 拖动部分
tableview.dragInteractionEnabled = true
tableview.dragDelegate = self
tableview.dropDelegate = self

增加逻辑支持

extension ViewController: UITableViewDragDelegate, UITableViewDropDelegate {
    func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
        
    }
    
    func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
        let dragItem = UIDragItem(itemProvider: NSItemProvider())
        dragItem.localObject = datas[indexPath.row]
        return [dragItem]
    }
    
    func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        let mover = datas.remove(at: sourceIndexPath.row)
        datas.insert(mover, at: destinationIndexPath.row)
    }
}

原文地址:https://blog.csdn.net/xo19882011/article/details/128441679

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_16479.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注