CollectionView 的UserDefaults 中调用拖放后如何更新数组索引?
How to Update Array Index After Drag and Drop is Called in UserDefaults in CollectionView?
我正在创建一个演示,其中我有拖放功能,拖放功能工作得很好,我担心的是,当我关闭应用程序并再次返回视图时,同样是索引显示在数组中,我想像在索引处拖放一样获取数组。
这是我的代码
CollectionView 方法
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
cell.lbl.text = array[indexPath.row]
cell.img.layer.cornerRadius = cell.img.frame.size.width / 2
cell.lbl.layer.cornerRadius = cell.lbl.frame.size.width / 2
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.size.width / 2) - 20, height: 200)
}
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = array.remove(at: sourceIndexPath.item)
defaults.removeObject(forKey: "data")
array.insert(item, at: destinationIndexPath.item)
defaults.set(item, forKey: "data")
defaults.synchronize()
print(item )
}
View 加载和 LongPressGesture 方法
@IBOutlet weak var collectionView: UICollectionView!
var array = ["Fateh", "Parul", "Jitendra", "Javed", "Brijesh","Pandey Ji"]
let defaults = UserDefaults.standard
var longPress : UILongPressGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if UserDefaults.standard.array(forKey: "data") == nil {
// userDefault has a value
defaults.set(array, forKey: "data")
} else {
let newData = defaults.object(forKey: "data")
array = newData as! [String]
}
collectionView.delegate = self
collectionView.dataSource = self
longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture(gesture:)))
collectionView.addGestureRecognizer(longPress)
}
@objc func handleLongGesture(gesture : UILongPressGestureRecognizer){
switch gesture.state {
case .began:
guard let selectedIndex = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {
break
}
collectionView.beginInteractiveMovementForItem(at: selectedIndex)
case .changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view))
case .ended:
collectionView.endInteractiveMovement()
defaults.synchronize()
@unknown default:
collectionView.cancelInteractiveMovement()
}
}
在 viewDidLoad
中,如果 UserDefaults 中没有已保存的数据,则您正在这样做:
defaults.set(array, forKey: "data")
您正在将 数组 设置为数据。
在 moveItemAt
中,您正在这样做:
defaults.set(item, forKey: "data")
您仅将单个 item 设置为数据,而您应该将更新后的 array 设置为数据。
作为旁注,不需要 defaults.synchronize()
...来自 Apple 的 docs:
synchronize()
Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.
我正在创建一个演示,其中我有拖放功能,拖放功能工作得很好,我担心的是,当我关闭应用程序并再次返回视图时,同样是索引显示在数组中,我想像在索引处拖放一样获取数组。
这是我的代码
CollectionView 方法
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return array.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCell", for: indexPath) as! ImageCell
cell.lbl.text = array[indexPath.row]
cell.img.layer.cornerRadius = cell.img.frame.size.width / 2
cell.lbl.layer.cornerRadius = cell.lbl.frame.size.width / 2
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.size.width / 2) - 20, height: 200)
}
func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = array.remove(at: sourceIndexPath.item)
defaults.removeObject(forKey: "data")
array.insert(item, at: destinationIndexPath.item)
defaults.set(item, forKey: "data")
defaults.synchronize()
print(item )
}
View 加载和 LongPressGesture 方法
@IBOutlet weak var collectionView: UICollectionView!
var array = ["Fateh", "Parul", "Jitendra", "Javed", "Brijesh","Pandey Ji"]
let defaults = UserDefaults.standard
var longPress : UILongPressGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if UserDefaults.standard.array(forKey: "data") == nil {
// userDefault has a value
defaults.set(array, forKey: "data")
} else {
let newData = defaults.object(forKey: "data")
array = newData as! [String]
}
collectionView.delegate = self
collectionView.dataSource = self
longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleLongGesture(gesture:)))
collectionView.addGestureRecognizer(longPress)
}
@objc func handleLongGesture(gesture : UILongPressGestureRecognizer){
switch gesture.state {
case .began:
guard let selectedIndex = collectionView.indexPathForItem(at: gesture.location(in: collectionView)) else {
break
}
collectionView.beginInteractiveMovementForItem(at: selectedIndex)
case .changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view))
case .ended:
collectionView.endInteractiveMovement()
defaults.synchronize()
@unknown default:
collectionView.cancelInteractiveMovement()
}
}
在 viewDidLoad
中,如果 UserDefaults 中没有已保存的数据,则您正在这样做:
defaults.set(array, forKey: "data")
您正在将 数组 设置为数据。
在 moveItemAt
中,您正在这样做:
defaults.set(item, forKey: "data")
您仅将单个 item 设置为数据,而您应该将更新后的 array 设置为数据。
作为旁注,不需要 defaults.synchronize()
...来自 Apple 的 docs:
synchronize()
Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.