【问题标题】:Saving reordered uicollectionview cell not working without error保存重新排序的 uicollectionview 单元格无法正常工作
【发布时间】: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


    【解决方案1】:

    我 相信您必须使用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()
            }
    

    【讨论】:

    • 我已经添加了重新加载数据方法,但仍然无法正常工作@Alsh
    • 尝试打印keyString数组,看看是否按新顺序保存
    • 并打印目标索引和源索引以确保它不是同一个索引
    • 它以新顺序正确保存@Alsh
    • 确保您显示的是正确的 collectionView,我找不到任何其他东西阻止按照您给它的顺序显示数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2017-06-09
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多