简体   繁体   English

保存重新排序的uicollectionview单元无法正常工作

[英]Saving reordered uicollectionview cell not working without error

I can finally reorder the cells sucessfully in the collectionview, but I can't save despite having UserDefaults after reordering it and see the collectionview in it's reordered state after restarting the app. 我终于可以在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]
        }
    }

I believe you have to reload the collectionView data using reloadData() method 我相信您必须使用reloadData()方法重新加载collectionView数据

let orderkeystring = UserDefaults.standard.object(forKey:"keystringreorder")
        if(orderkeystring != nil) {
            keystring = orderkeystring as! [String]
            // Don't forget to reload data here 
            1configcollectionview,reloadData()
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM