我有 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
数据,以便我可以在索引路径的 didselcect 项目中发送数据
最佳答案
如果对您有帮助,请尝试使用这些案例
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
关于ios - 如何在 swift 4 的 didSelectItemAt IndexPath 中的数组中嵌入字典类型数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622618/