在我的情况下,我将storing
数组数据放入UserDefault
中的VC2
中。在另一个VC1
中,我是retrieving
数据。现在,我需要将数据加载到VC1中的collectionview
数组中。在这里,我正在使用多个数据源,而不是想要使用单个数据源来获取要加载到collectionview的特定值。如何实现呢?
注意:关闭VC2
时,我需要将数组值传递给VC1
在VC2中将数组数据存储到UserDefault中
let selectedData = membersData.filter{$0.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)]*/
答案 0 :(得分:1)
VC2中的以下代码
在VC2中将数组数据存储到UserDefault中
let selectedData = membersData.filter{$0.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:[TeamListData]? //实例方法 var委托: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)
}
答案 1 :(得分:-1)
使用存档保存数据,并使用UserDefaults中的Unarchive获取数据
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()
}
}