Swift UserDefault 存储的数组数据加载到 UICollectionView 中

Swift UserDefault stored array data load into UICollectionView

在我的场景中,我是storing数组数据到UserDefault中的VC2。在另一个 VC1 我是 retrieving 数据。现在,我需要将数据加载到 VC1 中的 collectionview 数组中。在这里,我使用多个数据源而不是想使用单个数据源来获取特定值以加载到 collectionview 中。如何实现?

注意:在关闭 VC2 时,我需要将数组值传递给 VC1

数组数据存入VC2中的UserDefault

let selectedData = membersData.filter{[=11=].isSelected}
UserDefaults.standard.set(try? PropertyListEncoder().encode(selectedData), forKey:"sessiondata")

正在 VC1 中检索存储数据

    func storeValidaion(){
         // Retrive Array Values
          if let data = UserDefaults.standard.value(forKey:"sessiondata") as? Data {
               let sessionData = try? PropertyListDecoder().decode(Array<TeamListData>.self, from: data)
               print("ARRAY VALUES: \(sessionData!)")

          // here how to load array data into collection view

   if sessionData?.isEmpty == false {
            for person in sessionData! {
                print(person.userid!)

                if useridArray.contains(person.userid!) {
                    print("yes")
                } else {
                    self.useridArray.append(person.userid!)
                    self.nameArray.append(person.firstname!)
                    self.profileArray.append(person.profileimage!)
                    print(useridArray)
                    print(profileArray)
                }
                self.collectionView.reloadData()
            }
        } else {
            UserDefaults.standard.removeObject(forKey:"sessiondata")
            self.useridArray.removeAll()
            self.profileArray.removeAll()
            self.nameArray.removeAll()
            self.collectionView.reloadData()
        }
    }
    }

/*ARRAY VALUES: [ListData(userid: Optional("1"), firstname: Optional("abc"), designation: Optional("student"), profileimage: Optional("url"), isSelected: false), ListData(userid: Optional("2"), firstname: Optional("def"), designation: Optional("Executive"), profileimage: Optional("url"), isSelected: false)]*/

在 UserDefaults 中使用存档保存数据并使用取消存档获取数据

 func saveCustomData() throws {
    let array = ["New","Old"]
    let encodedData = try NSKeyedArchiver.archivedData(withRootObject: array, requiringSecureCoding: false)
    UserDefaults.standard.set(encodedData, forKey: "data")
}

func retrieveData() throws {
    if let data = UserDefaults.standard.data(forKey: "data"),
        let dataArr = try  NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(data) as? [String] {
    } else {
        print("There is an issue")
        self.saveCustomData()
    }
}

下面是 VC2 中的代码

数组数据存入VC2中的UserDefault

let selectedData = membersData.filter{[=10=].isSelected}
UserDefaults.standard.set(try? PropertyListEncoder().encode(selectedData), forKey:"sessiondata")

采用 VC2Delegate

func dismissProtocole(data: Any) {
        <#code#>
    }

在 VC1 中显示 VC2

if let details = self.storyboard?.instantiateViewController(withIdentifier: "VC2") as?VC2 {
            details.modalPresentationStyle = .fullScreen
             details.modalTransitionStyle = .crossDissolve
            **details.selectedData = selectedData** // pass data
             details.delegate = self
            self.navigationController?.present(details, animated: true, completion: nil)
        }

VC2

var selectedData:[团队列表数据]? // 实例方法 var delegate : VC2Delegate?

在 VC2 中检索存储的数据

func storeValidaion(){
        // Retrive Array Values
        if selectedData == nil  {
            if let data = UserDefaults.standard.value(forKey:"sessiondata") as? Data {
                guard let sessionData = try? PropertyListDecoder().decode(Array<TeamListData>.self, from: data) else {
                    return
                }

                print("ARRAY VALUES: \(sessionData)")
                self.selectedData = sessionData
                self.collectionView.reloadData()
                // put your code whatever you want....
            }
        }

    }

关闭方法

func dismiss() {
    self.delegate?.dismissProtocole(data: sessionData)
}

协议声明

protocol VC2Delegate {
    func dismissProtocole( data : Any)
}