【问题标题】:How to embed dictionary type data in array in didSelectItemAt IndexPath in swift 4?如何在swift 4中将字典类型数据嵌入到didSelectItemAt IndexPath的数组中?
【发布时间】:2018-04-03 05:07:29
【问题描述】:

我有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 的didelcect 项目中发送数据

【问题讨论】:

  • didSelectItemAt 你在最后一行调用了show func 那么什么是show func??
  • @dahiya_boy 。先生,请您详细说明
  • didSelectItemAt 中,你在最后一行调用了 show func 那么 show func 是什么??
  • 负责showViewController的@dahiya_boy
  • 只想从字典中获取值作为数组?或将带有键及其值的完整字典转换为数组?

标签: ios arrays swift nsdictionary swift4


【解决方案1】:

如果对您有帮助,请尝试使用这些案例

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!

带有 cmets 的示例代码请检查

https://drive.google.com/open?id=1rWeg6PMHQH2oL0wMS1ed9fGkHkvLAIrP

【讨论】:

  • 我使用模型类文件来解析 json 我正在寻找的是将字典类型数据嵌入到数组中。请帮忙
  • 您想将字典直接添加为数组吗?还是只有价值观?
  • 您正在使用模型类 Right 来解析 json,这意味着您也可以将获取的特定 UserDetail 对象传递给其他控制器,对吧?
  • 实际上我想将全部 userDetails 数据发送到 collectionview didselect 中的另一个视图控制器
  • 这很简单,您已经在使用模态类,只需将作为对象启动的模态类实例在 Dictionary 的位置传递给其他类,就在我的代码发布案例 2 中
【解决方案2】:

创建来自http://www.json4swift.com/的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)

【讨论】:

  • 我只使用对象映射器进行 json 解析。我已经创建了模型类。你能告诉我什么是modelsFromDictionaryArray 先生@Manish Mahajan
  • 浏览我的第一行...我说创建 userDetails 响应的模型。只要您将 JSON 响应放入 json4swift.com,用户类文件就会自动生成
  • 好的,先生这样做
【解决方案3】:

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)

【讨论】:

  • 但我无法将 userDetails 数据嵌入 your_array @dahiya_boy
  • @GoribDeveloper 你没有向我展示任何与此相关的东西。所以首先告诉我你做了什么。然后我会根据它编辑并重新回答你。
  • 看,先生,我有一个 api。在 json 解析之后,我得到了数据。得到数据后,我正在显示详细信息集合视图。像这样 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return userDetails.user_medias.count }
  • 先生:现在我只想将 userDetails 数据嵌入到一个数组中,以便我可以在 indexpath 中将它发送到另一个视图控制器
  • @GoribDeveloper 请告诉我CELLFORITEM FUNC。添加您的问题。
猜你喜欢
  • 2015-08-30
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
相关资源
最近更新 更多