以键值格式将值发送到另一个视图

时间:2017-11-07 06:19:50

标签: ios swift

我的tableviewcells中显示了一些值,如此......

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "abc") as! sellTableViewCell

        let product = arrProduct?[indexPath.section]
        cell.rateTextField.text = product?.theRate

  //Showing images in imageView using SDWebImage
        cell.prdImgView.sd_setImage(with: arrProduct?[indexPath.section].images[indexPath.row].url, placeholderImage: UIImage(named: "appLogo.jpg"), options: []) { (image, error, imageCacheType, imageUrl) in
        self.appDelegate.commonArrayOfSelectedImages.append(image!)}}

此处,product?.theRate具有费率值& self.appDelegate.commonArrayOfSelectedImages包含所有图片。现在,如果我总共有5个图像,那么第一个图像将具有一个速率,第二个图像将具有另一个速率,依此类推。我想以这种格式将这些值一起传递给另一个viewcontroller,以便我可以访问第一个图像&其对应的速率,第二图像及其对应的速率&等等...但是如何实现我不确定......

2 个答案:

答案 0 :(得分:0)

只需创建一个Dictionary数组即可。此词典将具有两个键值对。 IST Key for Image和2nd for Product Rate并将此Dictionary存储在数组中,就像存储在app-delegate数组中一样。就像是     var dicSet:NSMutableDictionary!     dicSet.setObject(image!,forKey:" Image")     dicSet.setObject(rate,forKey:" Rate")     self.appDelegate.commonArrayOfSelectedImages.append(dicSet)}}

答案 1 :(得分:0)

您必须创建一个字典并添加带有相应数据的图像。

var dataDictionary: [String:String] = [String:String]() //declare it globally in a class


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell: sellTableViewCell = tableView.dequeueReusableCell(withIdentifier: "abc") as! sellTableViewCell

    let product = arrProduct?[indexPath.section]
    cell.rateTextField.text = product?.theRate

    //Showing images in imageView using SDWebImage
    cell.prdImgView.sd_setImage(with: arrProduct?[indexPath.section].images[indexPath.row].url, placeholderImage: UIImage(named: "appLogo.jpg"), options: []) { (image, error, imageCacheType, imageUrl) in
        self.appDelegate.commonArrayOfSelectedImages.append(image!)}
    self.dataDictionary[arrProduct?[indexPath.section].images[indexPath.row].url] = product?.theRate
}

现在,如果您想通过点击按钮或其他内容发送所有数据,请执行以下操作:

@IBAction func clickOnButton(_ sender: UIButton) {

    let vc = self.storyboard!.instantiateViewController(withIdentifier: "YourViewController") as! YourViewController
    vc.dictionay = self.dataDictionary // you will get dictionary with name of image with corrosponding data 
    self.navigationController!.pushViewController(vc, animated: true)
}

你会得到一个这样的字典: [ “imageName1”: “RATE1”, “imageName2”: “率2”, “imageName3”: “率3”]