CollectionViewのcellの並べ替えを保存したいです。
並べ替えられる機能は実装できたのですが、並べ替えが保存されずビルドし直すと、並べ替え前の順番に戻ってしまっています。
並び順を保持する方法は何かありませんか?
ご教示よろしくお願いします。
swift
1import UIKit 2 3class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 4 5 var collectionView: UICollectionView? 6 var longPressGesture: UILongPressGestureRecognizer? 7 var selectedIndexPath: NSIndexPath? 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 self.setupSubviews() 12 self.autolayoutSubviews() 13 14 self.navigationItem.rightBarButtonItem = self.editButtonItem() 15 } 16 17 func setupSubviews() { 18 19 let flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 20 flowLayout.minimumInteritemSpacing = 10.0 21 flowLayout.minimumLineSpacing = 10.0 22 flowLayout.sectionInset = UIEdgeInsets(top: 10.0, left: 10.0, bottom: 10.0, right: 10.0) 23 flowLayout.itemSize = CGSizeMake(300.0, 100.0) 24 25 self.collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout) 26 self.collectionView!.translatesAutoresizingMaskIntoConstraints = false 27 self.collectionView!.dataSource = self 28 self.collectionView!.delegate = self 29 30 self.collectionView!.registerClass(FirstCell.self, forCellWithReuseIdentifier: "FirstCell") 31 self.collectionView!.registerClass(SecondCell.self, forCellWithReuseIdentifier: "SecondCell") 32 self.collectionView!.registerClass(ThirdCell.self, forCellWithReuseIdentifier: "ThirdCell") 33 34 self.collectionView!.backgroundColor = UIColor.whiteColor() 35 self.view.addSubview(self.collectionView!) 36 37 longPressGesture = UILongPressGestureRecognizer(target: self, action: 38 #selector(ViewController.handleLongGesture(_:))) 39 40 41 longPressGesture?.minimumPressDuration = 0.5 42 43 self.longPressGesture!.enabled = false 44 45 46 self.collectionView?.addGestureRecognizer(self.longPressGesture!) 47 } 48 49 func autolayoutSubviews() { 50 51 self.collectionView!.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true 52 self.collectionView!.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true 53 self.collectionView!.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true 54 self.collectionView!.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true 55 } 56 57 58 override func setEditing(editing: Bool, animated: Bool) { 59 super.setEditing(editing, animated: animated) 60 61 self.collectionView!.allowsMultipleSelection = editing 62 let indexPaths: [NSIndexPath] = self.collectionView!.indexPathsForVisibleItems() 63 64 for indexPath in indexPaths { 65 self.collectionView!.deselectItemAtIndexPath(indexPath, animated: false) 66 let cell: UICollectionViewCell = self.collectionView!.cellForItemAtIndexPath(indexPath)! 67 68 } 69 70 if editing { 71 self.longPressGesture!.enabled = true 72 } 73 else { 74 self.longPressGesture!.enabled = false 75 } 76 77 } 78 79 80 81 func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 82 return 3 83 } 84 85 86 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 87 let cellTypeNumber = indexPath.item % 3 88 switch cellTypeNumber { 89 case 0: 90 let cell1 = collectionView.dequeueReusableCellWithReuseIdentifier("FirstCell", forIndexPath: indexPath) as! FirstCell 91 cell1.backgroundColor = UIColor.redColor() 92 return cell1 93 94 case 1: 95 let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("SecondCell", forIndexPath: indexPath) as! SecondCell 96 cell2.backgroundColor = UIColor.yellowColor() 97 return cell2 98 99 default: 100 let cell3 = collectionView.dequeueReusableCellWithReuseIdentifier("ThirdCell", forIndexPath: indexPath) as! ThirdCell 101 cell3.backgroundColor = UIColor.greenColor() 102 return cell3 103 } 104 } 105 106 func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { 107 } 108 109 110 func handleLongGesture(gesture: UILongPressGestureRecognizer) { 111 112 switch(gesture.state) { 113 case UIGestureRecognizerState.Began: 114 guard let selectedIndexPath = collectionView?.indexPathForItemAtPoint(gesture.locationInView(collectionView!)) else { 115 break 116 } 117 collectionView?.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath) 118 case UIGestureRecognizerState.Changed: 119 collectionView?.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!)) 120 case UIGestureRecognizerState.Ended: 121 collectionView?.endInteractiveMovement() 122 default: 123 collectionView?.cancelInteractiveMovement() 124 } 125 126 127 128 } 129 130 131 132} 133
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/09/05 06:36
2016/09/05 12:36
退会済みユーザー
2016/09/06 01:41