问题描述
在我的场景中,我将存储
数组数据到VC2
中的UserDefault
中.在另一个 VC1
中,我正在 retrieving
数据.现在,我需要将数据加载到 VC1 中的 collectionview
数组中.在这里,我使用多个数据源而不是想要使用单个数据源来获取要加载到 collectionview 的特定值.如何实现这一目标?
In my scenario, I am storing
array data into UserDefault
in VC2
. In another VC1
I am retrieving
data. Now, I need to load data into collectionview
array in VC1. Here, I am using multiple datasource Instead of that want to use single data source to get a specific values to load into collectionview. How to achieve this?
注意:在关闭 VC2
时,我需要将数组值传递给 VC1
NOTE: while dismissing VC2
I need to pass the array value to 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)]*/
推荐答案
下面是VC2里面的代码
Below code inside VC2
数组数据存储到 VC2 中的 UserDefault
let selectedData = membersData.filter{$0.isSelected}
UserDefaults.standard.set(try? PropertyListEncoder().encode(selectedData), forKey:"sessiondata")
采用 VC2Delegate
adopt 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?
var selectedData : [TeamListData]? // instance method 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)
}
这篇关于Swift UserDefault 存储数组数据加载到 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!