第五天: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.pswp.cn/news/252455.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252455.shtml
英文地址,請注明出處:http://en.pswp.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…