如何在 swift 4 中的 didSelectItemAt IndexPath 中的数组中嵌入字典类型数据?
How to embed dictionary type data in array in didSelectItemAt IndexPath in swift 4?
我有 userDetails
数据。现在我想将 didSelectItemAt IndexPath
中的 userDetails
发送给另一个 viewController
。
怎么做?
这是我的一段代码
var userDetails : UserDetails!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
let imageUrl = URL(string: Constant.url.imageBaseUrl + userDetails.user_medias[indexPath.row].thumbnail_url)
cell.imgvw.kf.setImage(with: imageUrl)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DetailsVC) as! DetailsVC
vc.userDetails = userList[indexPath.row]
self.show(vc, sender: self)
}
// in number of rows in section i am showing this
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return userDetails.user_medias.count
}
如何在 userList
中嵌入 userDetails
数据,以便我可以在 indexpath
的 didselcect 项目中发送数据
在DetailsVC
中创建对象
var myUser : [String : Any] = [:]
在ViewDidLoad
let id = myUser["id"]
let userId = myUser["user_id"]
let catId = myUser["category_id"]
在 FirstVCdidselect
let vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DetailsVC) as! DetailsVC
vc.myUser = userDetails.user_medias[indexPath.row] as! [String : Any]
self.show(vc, sender: self)
创建 userDetails 响应模型
声明你的用户类型数组
var yourArray = [User]()
在API响应中使用下面的代码:-
if let data = data["userDetails"] as? NSArray {
self.yourArray.append(contentsOf: User.modelsFromDictionaryArray(array: data))
}
在 didSelect 方法中使用下面的代码:-
let vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DetailsVC) as! DetailsVC
vc.userDetails = yourArray[indexPath.row]
self.show(vc, sender: self)
如果对您有帮助,请尝试使用这些案例
var myDict : [String : String] = ["ios":"1","Geek":"2","IosGeek":"4"]
///First case - Map the Dict
let onlyValues = myDict.map({[=10=].1})
print(onlyValues) // Print Only Values
///Second Case - Using a Seprate class
class sortData
{
var iosVal : String!
var geekVal : String!
var iosGeekVal : String!
init(parameter : [String:String]) {
iosVal = parameter["ios"]
geekVal = parameter["Geek"]
iosGeekVal = parameter["IosGeek"]
}
}
class DestinationVC : UIViewController {
var passedData : sortData!
}
///Getting Data Using Class
var sortingData : sortData?
sortingData = sortData.init(parameter: myDict)
print(sortingData?.iosVal!) // "1"
/// pass this class in Your destinationVC
/// And Use as
/// sortingData?.iosVal! // sortingData?.geekVal! // sortingData?.iosGeekVal!
带注释的示例代码请检查
https://drive.google.com/open?id=1rWeg6PMHQH2oL0wMS1ed9fGkHkvLAIrP
我有 userDetails
数据。现在我想将 didSelectItemAt IndexPath
中的 userDetails
发送给另一个 viewController
。
怎么做?
这是我的一段代码
var userDetails : UserDetails!
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
let imageUrl = URL(string: Constant.url.imageBaseUrl + userDetails.user_medias[indexPath.row].thumbnail_url)
cell.imgvw.kf.setImage(with: imageUrl)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DetailsVC) as! DetailsVC
vc.userDetails = userList[indexPath.row]
self.show(vc, sender: self)
}
// in number of rows in section i am showing this
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return userDetails.user_medias.count
}
如何在 userList
中嵌入 userDetails
数据,以便我可以在 indexpath
在DetailsVC
中创建对象
var myUser : [String : Any] = [:]
在ViewDidLoad
let id = myUser["id"]
let userId = myUser["user_id"]
let catId = myUser["category_id"]
在 FirstVCdidselect
let vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DetailsVC) as! DetailsVC
vc.myUser = userDetails.user_medias[indexPath.row] as! [String : Any]
self.show(vc, sender: self)
声明你的用户类型数组
var yourArray = [User]()
在API响应中使用下面的代码:-
if let data = data["userDetails"] as? NSArray {
self.yourArray.append(contentsOf: User.modelsFromDictionaryArray(array: data))
}
在 didSelect 方法中使用下面的代码:-
let vc = self.storyboard!.instantiateViewController(withIdentifier: Constant.vc.DetailsVC) as! DetailsVC
vc.userDetails = yourArray[indexPath.row]
self.show(vc, sender: self)
如果对您有帮助,请尝试使用这些案例
var myDict : [String : String] = ["ios":"1","Geek":"2","IosGeek":"4"]
///First case - Map the Dict
let onlyValues = myDict.map({[=10=].1})
print(onlyValues) // Print Only Values
///Second Case - Using a Seprate class
class sortData
{
var iosVal : String!
var geekVal : String!
var iosGeekVal : String!
init(parameter : [String:String]) {
iosVal = parameter["ios"]
geekVal = parameter["Geek"]
iosGeekVal = parameter["IosGeek"]
}
}
class DestinationVC : UIViewController {
var passedData : sortData!
}
///Getting Data Using Class
var sortingData : sortData?
sortingData = sortData.init(parameter: myDict)
print(sortingData?.iosVal!) // "1"
/// pass this class in Your destinationVC
/// And Use as
/// sortingData?.iosVal! // sortingData?.geekVal! // sortingData?.iosGeekVal!
带注释的示例代码请检查
https://drive.google.com/open?id=1rWeg6PMHQH2oL0wMS1ed9fGkHkvLAIrP