【发布时间】:2018-09-27 00:12:10
【问题描述】:
我终于可以在 collectionview 中成功地重新排序单元格,但是我无法保存,尽管在重新排序后有 UserDefaults 并在重新启动应用程序后看到处于重新排序状态的 collectionview。
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
if collectionView == 1configcollectionview {
let itemToMove = keystring[sourceIndexPath.row]
keystring.remove(at: sourceIndexPath.row)
keystring.insert(itemToMove, at: destinationIndexPath.row)
print("Starting Index: \(sourceIndexPath.row)")
print("Ending Index: \(destinationIndexPath.row)")
UserDefaults.standard.set(keystring, forKey:"keystringreorder")
// let movekey = keystring.remove(at: sourceIndexPath.item)
// keystring.insert(movekey, at: destinationIndexPath.item)
}
if collectionView == 2configcollectionview {
let movekey1 = keystring1.remove(at: sourceIndexPath.item)
keystring1.insert(movekey1, at: destinationIndexPath.item)
print("Starting Index: \(sourceIndexPath.item)")
print("Ending Index: \(destinationIndexPath.item)")
UserDefaults.standard.set(keystring1, forKey:"keystringreorder1")
}
if collectionView == 3configcollectionview {
let movekey2 = keystring2.remove(at: sourceIndexPath.item)
keystring2.insert(movekey2, at: destinationIndexPath.item)
print("Starting Index: \(sourceIndexPath.item)")
print("Ending Index: \(destinationIndexPath.item)")
UserDefaults.standard.set(keystring2, forKey:"keystringreorder2")
}
}
override func viewDidLoad() {
super.viewDidLoad()
let orderkeystring = UserDefaults.standard.object(forKey:"keystringreorder")
if(orderkeystring != nil) {
keystring = orderkeystring as! [String]
}
}
【问题讨论】:
标签: ios iphone swift3 uicollectionview