我有userDetails
个数据。现在我想将userDetails
中的didSelectItemAt IndexPath
发送给另一个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
}
如何在userDetails
中嵌入userList
数据,以便我可以在indexpath的didselcect项目中发送数据
答案 0 :(得分:2)
尝试使用这些案例,如果它可以帮助你
var myDict : [String : String] = ["ios":"1","Geek":"2","IosGeek":"4"]
///First case - Map the Dict
let onlyValues = myDict.map({$0.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
答案 1 :(得分:0)
声明您的用户类型的阵列
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)
答案 2 :(得分:-1)
在DetailsVC
创建对象
var myUser : [String : Any] = [:]
ViewDidLoad
中的
let id = myUser["id"]
let userId = myUser["user_id"]
let catId = myUser["category_id"]
在FirstVC中didselect
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)