第五天:Swift拖动 item 重排 CollectionView

 参考链接:https://www.jianshu.com/p/96f956f1479e

                                

  1 import UIKit
  2 
  3 enum VC: String {
  4     case ViewController
  5     case CollectionViewController
  6     
  7     func segueIdentifier() -> String {
  8         switch self {
  9         case .ViewController:
 10             return "SegueIdentifierViewController"
 11         case .CollectionViewController:
 12             return "SegueIdentifeirCollectionViewController"
 13         }
 14     }
 15 }
 16 
 17 private let CellID = "CellId"
 18 
 19 class MCMainTableViewController: UITableViewController {
 20 
 21     lazy var tempArr: [VC] = {
 22         var arr: [VC] = [.ViewController, .CollectionViewController];
 23         return arr
 24     }()
 25     
 26     override func viewDidLoad() {
 27         super.viewDidLoad()
 28 
 29         
 30         // Uncomment the following line to preserve selection between presentations
 31         // self.clearsSelectionOnViewWillAppear = false
 32 
 33         // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 34         // self.navigationItem.rightBarButtonItem = self.editButtonItem
 35     }
 36 
 37     override func didReceiveMemoryWarning() {
 38         super.didReceiveMemoryWarning()
 39         // Dispose of any resources that can be recreated.
 40     }
 41 
 42     // MARK: - Table view data source
 43 
 44     override func numberOfSections(in tableView: UITableView) -> Int {
 45         // #warning Incomplete implementation, return the number of sections
 46         return 1
 47     }
 48 
 49     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
 50         // #warning Incomplete implementation, return the number of rows
 51         return tempArr.count
 52     }
 53 
 54     
 55     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 56         let cell = tableView.dequeueReusableCell(withIdentifier: CellID, for: indexPath)
 57 
 58         // Configure the cell...
 59         cell.textLabel?.text = tempArr[indexPath.row].rawValue
 60 
 61         return cell
 62     }
 63     
 64     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 65         self.performSegue(withIdentifier: tempArr[indexPath.row].segueIdentifier(), sender: tableView)
 66     }
 67     
 68     /*
 69     // Override to support conditional editing of the table view.
 70     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
 71         // Return false if you do not want the specified item to be editable.
 72         return true
 73     }
 74     */
 75 
 76     /*
 77     // Override to support editing the table view.
 78     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
 79         if editingStyle == .delete {
 80             // Delete the row from the data source
 81             tableView.deleteRows(at: [indexPath], with: .fade)
 82         } else if editingStyle == .insert {
 83             // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 84         }    
 85     }
 86     */
 87 
 88     /*
 89     // Override to support rearranging the table view.
 90     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
 91 
 92     }
 93     */
 94 
 95     /*
 96     // Override to support conditional rearranging of the table view.
 97     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
 98         // Return false if you do not want the item to be re-orderable.
 99         return true
100     }
101     */
102 
103 
104     // MARK: - Navigation
105 
106     // In a storyboard-based application, you will often want to do a little preparation before navigation
107     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
108         // Get the new view controller using segue.destinationViewController.
109         // Pass the selected object to the new view controller.
110         
111         for index in 0..<tempArr.count {
112             if segue.identifier == tempArr[index].segueIdentifier() {
113                 segue.destination.title = tempArr[index].rawValue
114                 break
115             }
116         }
117     }
118 
119 }
 1 import UIKit
 2 
 3 private let CellId: String = "CellId"
 4 
 5 class MCViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
 6 
 7     lazy var tempArr: [String] = {
 8         var arr: [String] = []
 9         for i in 0..<100{
10             let tempStr = "\(i)"
11             arr.append(tempStr)
12         }
13         return arr
14     }()
15     
16     @IBOutlet weak var collectionView: UICollectionView!
17     
18     override func viewDidLoad() {
19         super.viewDidLoad()
20 
21         // Do any additional setup after loading the view.
22         
23         let longGestureRecognizer: UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(longPressAction(_:)))
24         self.collectionView.addGestureRecognizer(longGestureRecognizer)
25     }
26 
27     @objc func longPressAction(_ longPressGes: UILongPressGestureRecognizer) {
28         switch longPressGes.state {
29         case .began:
30             guard let selectIndexPath = collectionView.indexPathForItem(at: longPressGes.location(in: collectionView)) else { return }
31             collectionView.beginInteractiveMovementForItem(at: selectIndexPath)
32         case .changed:
33             collectionView.updateInteractiveMovementTargetPosition(longPressGes.location(in: collectionView))
34         case .ended:
35             collectionView.endInteractiveMovement()
36         default:
37             collectionView.cancelInteractiveMovement()
38         }
39     }
40     
41     override func didReceiveMemoryWarning() {
42         super.didReceiveMemoryWarning()
43         // Dispose of any resources that can be recreated.
44     }
45     
46 
47     /*
48     // MARK: - Navigation
49 
50     // In a storyboard-based application, you will often want to do a little preparation before navigation
51     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
52         // Get the new view controller using segue.destinationViewController.
53         // Pass the selected object to the new view controller.
54     }
55     */
56 
57 }
58 
59 extension MCViewController {
60     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
61         return tempArr.count
62     }
63     
64     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
65         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellId, for: indexPath) as! MCTextCollectionViewCell
66         
67         cell.textLabel.text = tempArr[indexPath.item]
68         
69         return cell
70     }
71     
72     func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
73         let tempCell = tempArr.remove(at: sourceIndexPath.item)
74         tempArr.insert(tempCell, at: destinationIndexPath.item)
75     }
76 }
  1 import UIKit
  2 
  3 private let reuseIdentifier: String = "CellID"
  4 
  5 class MCCollectionViewController: UICollectionViewController {
  6 
  7     lazy var tempArr: [String] = {
  8         var arr: [String] = []
  9         for i in 0..<100 {
 10             let str = "\(i)"
 11             arr.append(str)
 12         }
 13         return arr
 14     }()
 15     
 16     override func viewDidLoad() {
 17         super.viewDidLoad()
 18 
 19         // Uncomment the following line to preserve selection between presentations
 20         // self.clearsSelectionOnViewWillAppear = false
 21 
 22         // Register cell classes
 23         
 24 //        self.collectionView!.register(MCTextCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
 25 
 26         // Do any additional setup after loading the view.
 27     }
 28 
 29     override func didReceiveMemoryWarning() {
 30         super.didReceiveMemoryWarning()
 31         // Dispose of any resources that can be recreated.
 32     }
 33 
 34     /*
 35     // MARK: - Navigation
 36 
 37     // In a storyboard-based application, you will often want to do a little preparation before navigation
 38     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
 39         // Get the new view controller using [segue destinationViewController].
 40         // Pass the selected object to the new view controller.
 41     }
 42     */
 43 
 44     // MARK: UICollectionViewDataSource
 45 
 46 //    override func numberOfSections(in collectionView: UICollectionView) -> Int {
 47 //        // #warning Incomplete implementation, return the number of sections
 48 //        return 1
 49 //    }
 50 
 51 
 52     override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
 53         // #warning Incomplete implementation, return the number of items
 54         return tempArr.count
 55     }
 56 
 57     override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
 58         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MCTextCollectionViewCell
 59         
 60         // Configure the cell
 61         cell.textLabel.text = tempArr[indexPath.item]
 62     
 63         return cell
 64     }
 65     
 66     override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
 67         let tempCell = tempArr.remove(at: sourceIndexPath.item)
 68         tempArr.insert(tempCell, at: destinationIndexPath.item)
 69     }
 70 
 71     // MARK: UICollectionViewDelegate
 72 
 73     /*
 74     // Uncomment this method to specify if the specified item should be highlighted during tracking
 75     override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
 76         return true
 77     }
 78     */
 79 
 80     /*
 81     // Uncomment this method to specify if the specified item should be selected
 82     override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
 83         return true
 84     }
 85     */
 86 
 87     /*
 88     // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
 89     override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
 90         return false
 91     }
 92 
 93     override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
 94         return false
 95     }
 96 
 97     override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
 98     
 99     }
100     */
101 
102 }

 

转载于:https://www.cnblogs.com/chmhml/p/8338227.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/252455.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MIT Kimera阅读笔记

这两天在调研SLAM的最新算法&#xff0c;找到了2019CVPR上的一篇文章&#xff0c;出自于MIT&#xff0c;因为要给其他同事讲解&#xff0c;所以就把文章的重点内容在我个人理解的情况下翻译了出来&#xff0c;有理解不到位的还请各位大佬多多批评指正。 最后附上了Delaunay Tri…

C#中的ForEach

public void ForEach(Action<T> action) 针对List<T>集合中的每个元素执行操作Action<T> action,Action<T>是只接受一个类型为T的传入参数返回值为void的委托,对于泛型List<T>来说,Action<T>中的类型与List<T>中的类型是相同的.acti…

哈希映射

哈希来源问题&#xff1a;关于统计一个字符串集合中&#xff0c;求出现次数最多的字符串思路&#xff1a;建立一个哈希映射&#xff08;HashMap&#xff09;&#xff0c;其键为"字符串"&#xff0c;值为"字符串出现次数"&#xff0c;然后遍历字符串集合&am…

1月28日云栖精选夜读 | 终于等到你!阿里正式向 Apache Flink 贡献 Blink 源码

如同我们去年12月在 Flink Forward China 峰会所约&#xff0c;阿里巴巴内部 Flink 版本 Blink 将于 2019 年 1 月底正式开源。今天&#xff0c;我们终于等到了这一刻。 热点热议 终于等到你&#xff01;阿里正式向 Apache Flink 贡献 Blink 源码 作者&#xff1a;技术小能手 发…

ZOJ-3537

题目大意&#xff1a;给你一个n (n<300) 边形&#xff0c;给出它所有的顶点坐标&#xff0c;让你把它划分成n-2个三角形的花费最小值&#xff0c;顶点 a 和 b 相连的花费为 abs(a.xb.x)*abs(a.yb.y)。 如果是凹多边形输出无解。 思路&#xff1a;先跑个凸包判断是不是凸多边…

你会等待还是离开(大理)---写的一个推文

你会等待还是离开 -----出发和遇见大理 上关花闹 下关风薰 苍山雪寂 洱海月迟 但闻肆季弦雀起 才吹小雨又需晴 现实很调皮&#xff0c;很容易就让人没有力气&#xff0c;就像变与不变&#xff0c;并不复杂&#xff0c;也不遥远&#xff0c;一个寒假的距离&#xff0c;一句话的力…

sudo rosdep init ERROR: cannot download default sources list from: https://raw.githubusercontent.com

安装上ros无法进行rosdep init.解决方法如下&#xff1a;https://zhuanlan.zhihu.com/p/77483614 因此&#xff0c;在/usr/lib/python2.7/dist-packages/rosdep2/sources_list.py中顶部直接插入两行代码取消SSL验证 import ssl ssl._create_default_https_context ssl._crea…

YodaOS: 一个属于 Node.js 社区的操作系统

开发四年只会写业务代码&#xff0c;分布式高并发都不会还做程序员&#xff1f; >>> 大家好&#xff0c;很开心在这里宣布 YodaOS开源了。他将承载 Rokid 4年以来对于人工智能和语音交互领域的沉淀&#xff0c;并选择 Node.js 作为操作系统的一等开发公民&#xff0…

Android顶部粘至视图具体解释

不知从某某时间開始&#xff0c;这样的效果開始在UI设计中流行起来了。让我们先来看看效果&#xff1a;大家在支付宝、美团等非常多App中都有使用。要实现这个效果&#xff0c;我们能够来分析下思路&#xff1a;我们肯定要用2个一样的布局来显示我们的粘至布局。一个是正常的、…

在实际项目开发中keil的调试方法

转载2015-06-14 20:23:04 一.在keilc的调试状态下&#xff0c;如何观察各个片内外设的运行状态&#xff1f;如何修改它们的设置&#xff1f;​ 在调试状态下&#xff0c;点击Peripherals菜单下的不同外设选项命令&#xff0c;就会显示或隐藏对应外设的观察窗口。 在程序运行时&…

slam 常用数据集的帧率

1. kitti数据集的帧率约约为10fps,图像分辨率为1241x376 2. Euroc数据集的帧率约为20fps,图像分辨率为752x480 3.TUM数据集的帧率约为30fps, 图像分辨率为640x360 zed相机获取的HD图像的分辨率为1280x720p,获取的VGA图像分辨率为672x376,mynt相机获取的VGA图像的分辨率为640x…

小李飞刀:用python刷题ing....

叨逼叨 默认每天都要刷两道题。今天目标已完成。 第一题 26. 删除排序数组中的重复项难度&#xff1a;简单类型&#xff1a;数组 给定一个排序数组&#xff0c;你需要在原地删除重复出现的元素&#xff0c;使得每个元素只出现一次&#xff0c;返回移除后数组的新长度。不要使用…

【Log4J】

学习mybatis中用到了Log4J 在此记录下 引入 引入Maven配置 <!-- https://mvnrepository.com/artifact/log4j/log4j --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></de…

VI-ORB环境配置

参考博客:https://blog.csdn.net/qq_38589460/article/details/82559816 https://blog.csdn.net/Robot_Starscream/article/details/90245456 本机安装的是opencv3.0 在Examples/ROS/ORB-VIO以及/VI-ORB/src/LearnVIORB-RT下的CMakeLists.txt都要进行修改 将find_package(O…

.NET Core 3.0中的数据库驱动框架System.Data

虽然没有得到很多关注&#xff0c;但System.Data对于.NET中任何关系型数据库的访问都至关重要。因为其前身是ActiveX Data Objects&#xff0c;所以它也被称为ADO.NET。System.Data提供了一个通用框架&#xff0c;是构建.NET数据库驱动程序的基础。该框架提供了数据库驱动可以遵…

linux vg lv pv

pv由物理卷或者分区组成 pv可以组成一个或者多个vg vg可以分成多个lv 方便扩展 pvs vgs lvs 可以查看当前存在的pv vg lv 我的centos硬盘20g 使用了一段时间 加了100g 这时候 我们可以使用扩展来扩展我们的分区大小 查看自己拥有多少个硬盘 ls /dev/sd* | grep -v [0-9] …

mynt product model: D1000-IR-120标定相机和IMU外参

1. 首先是安装相应的mynt SDK. http://www.myntai.com/mynteye/depth小觅官网,在sdk下拉菜单中点击MYNT EYE Depth SDK,然后选择Linux Installation安装安装步骤说明一步步的安装,安装sample后,测试一下安装是否成功.我的电脑上安装了ROS,所以可以点击上面第一幅图中的ROS Ins…

吉林省第二条国际铁路联运大通道“长珲欧”启动测试

29日&#xff0c;吉林省第二条国际铁路联运大通道“长珲欧”在俄罗斯启动测试。吉林省商务厅供图 29日&#xff0c;吉林省第二条国际铁路联运大通道“长珲欧”在俄罗斯启动测试。吉林省商务厅供图 中新网长春1月29日电 (郭佳)记者29日从吉林省商务厅获悉&#xff0c;该省第二条…

使用Ajax解析数据遇到的问题

数据格式 我最近在使用JQuery的$.ajax访问后台的时候&#xff0c;发现竟然无法解析返回的数据&#xff0c;具体的错误记不清了(以后在遇到问题先截个图)&#xff0c;可以在浏览器的Console中看到一个错误&#xff0c;但是去看这条请求是有数据返回的&#xff0c;所以刚开始我一…

49、剑指offer--把字符串转换成整数

题目描述将一个字符串转换成一个整数&#xff0c;要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0 输入描述:输入一个字符串,包括数字字母符号,可以为空输出描述:如果是合法的数值表达则返回该数字&#xff0c;否则返回0输入例子:2147483647…