iOS 左滑编辑、长按拖动排序

简介: iOS 左滑编辑、长按拖动排序

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

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

效果图如下:
sample.gif

先把页面画起来

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)
    }
}
相关文章
|
存储 Java iOS开发
iOS 通讯录中文排序、全拼音排序
在做项目的时候,总遇到排序问题,英文排序是很简单的,直接使用compare方法就可以实现了,但是一旦遇到中文,就显得麻烦了。
225 0
|
开发工具 iOS开发
iOS 逆向编程(十一)iPhone 终端支持中文输入与vim命令(编辑文件)
iOS 逆向编程(十一)iPhone 终端支持中文输入与vim命令(编辑文件)
89 0
iOS8新特性扩展(Extension)应用之三——照片编辑插件
iOS8新特性扩展(Extension)应用之三——照片编辑插件
143 0
iOS8新特性扩展(Extension)应用之三——照片编辑插件
|
算法 iOS开发 搜索推荐
iOS 算法之排序、查找、递归
排序 冒泡排序(依次循环旁边的比较放到后边去) /** 最好时间复杂度是O(n) 最坏时间复杂度是O(n^2) 平均时间复杂度:O(n^2) 平均空间复杂度:O(1) */ - (void)foolSortArray:(NSMutableArray *)array { for (int i = 0; i < array.
977 0
|
监控 iOS开发
iOS版微信6.5.2发布 可简单编辑照片
国12月20日消息,在12月16日,iOS版微信刚更新至V6.5.1,支持在朋友圈分享相册中的视频。没过几天,腾讯又将版本号更新至6.5.2。在该版本中,用户终于能在选择照片时进行简单的编辑了,而这个功能在手机QQ早就有了。
935 0
|
自然语言处理 程序员 iOS开发
实战ios Plist 读 写操作及 增删改查 排序
pist 最为持久化保存的一种方式!本身plist文件是xml ,对于小数量数据可以采用plis 的方法!这样更高效!废话不多说了!进入正题吧!如果是一个新手用plist的话一定会遇到各种问题!我当时就是这样走过来的!也是做个总结吧! 功能介绍:保存学生的基本信息: 一. 在故事板上拖拽几个控件吧如下图用来输入学生的信息 并在ViewCo
2155 0
|
iOS开发
iOS中 UITableViewRowAction tableViewcell编辑状态下的功能 UI技术分享
 * tableView:editActionsForRowAtIndexPath: // 设置滑动删除时显示多个按钮  * UITableViewRowAction // 通过此类创建按钮  * 1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。
1180 0
|
图形学 iOS开发
iOS OpenGLES 框架相关 24 篇文档排序整理
iOS OpenGLES 框架相关 24 篇文档排序整理  太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循“署名-非商业用途-保持一致”创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。
1168 0