ios - 如何在 swift 4 的 didSelectItemAt IndexPath 中的数组中嵌入字典类型数据?

标签 ios arrays swift nsdictionary swift4

enter image description here enter image description here

我有 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/

相关文章:

iphone - 在录制的音频文件末尾添加静音

ios - Swift & Core Data - 将实体添加到现有实体

arrays - 在 Swift 3 中过滤 "2D"自定义对象数组

java - java中char数组到String数组的转换

ios - 在一个 View 中设置两个 UIPickerview

ios - 错误套接字 SO_NOAPNFALLBK 失败 : [42] Protocol not available, 转储回溯

android - 如何处理本地缓存的间隙

php - PHP list()错误: “Undefined offset”

ios - Swift Firebase 将 UID 键与 UID 字符串数组匹配

ios - 自发的 Swift 编译器错误