如何快速基于Json id从集合视图didSelectItemAt推送不同的视图控制器

问题描述 投票:0回答:2

我的项目包含collectionView ..但是如何基于json id从didSelectItemAt推送不同的viewcontroller。而且我为每个id具有单独的vewcontrollers。但是我无法使用基于json id的didSelectItemAt推送不同的viewcontrolls。

这是我的Json for collectionView:

{
"financer": [
    {
        "id": "45",
        "icon": "https://hello.com//images/img1.png"
    }
    {
        "id": "40",
        "icon": "https://hello.com//images/img2.png"
     }
     .
     .
     .
   ]
 }

这是我的家庭收藏查看代码:

import UIKit

struct JsonData {

var iconHome: String?
init(icon: String, tpe: String) {
    self.iconHome = icon
}
}

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var collectionView: UICollectionView!

var itemsArray = [JsonData]()
var idArray = [String]()
override func viewDidLoad() {
    super.viewDidLoad()

    homeServiceCall()
    //Do any additional setup after loading the view.
    collectionView.delegate = self
    collectionView.dataSource = self
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return itemsArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! HomeCollectionViewCell

    let aData = itemsArray[indexPath.row]
    cell.paymentLabel.text = aData.typeName

    if let url = NSURL(string: aData.iconHome ?? "") {
        if let data = NSData(contentsOf: url as URL) {
            cell.paymentImage.image = UIImage(data: data as Data)
        }
    }
    return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "MakePaymentViewController") as! MakePaymentViewController
    self.navigationController?.pushViewController(nextViewController, animated: true)
    let indexPathHome = indexPath.row
    print("home collectionItem indexpath \(indexPathHome)")

}

//MARK:- Service-call

func homeServiceCall(){

    let urlStr = "https://dev.com/webservices/getfinancer"
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

        guard let respData = data else {
            return
        }

        do{
            let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
            let financerArray = jsonObj["financer"] as! [[String: Any]]

            for financer in financerArray {

                let id = financer["id"] as! String
                let pic = financer["icon"] as? String

                self.itemsArray.append(JsonData(icon: pic ?? ""))
            }
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
        catch {
            print("catch error")
        }

    }).resume()
}
}

当我单击collectionview中的任何项目时,我可以推送相同的视图控制器,但是我需要根据json id推送不同的视图控制器。我不知道如何以及在哪里使用json id来使用didselectItem atIndexPath推送不同的viewcontroller。任何人请在这里帮助我。

json swift uicollectionview pushviewcontroller
2个回答
0
投票

更新您的homeServiceCall功能

func homeServiceCall(){

    let urlStr = "https://dev.com/webservices/getfinancer"
    let url = URL(string: urlStr)
    URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in

        guard let respData = data else {
            return
        }

        do{
            let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
            let financerArray = jsonObj["financer"] as! [[String: Any]]

            for financer in financerArray {

                let id = financer["id"] as! String
                let pic = financer["icon"] as? String

                self.itemsArray.append(JsonData(icon: pic ?? ""))
                self.idArray.append(id)
            }
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
        catch {
            print("catch error")
        }

    }).resume()
}

financerId中创建一个名为MakePaymentViewController的字符串属性

didSelect功能中

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "MakePaymentViewController") as? MakePaymentViewController {
        nextViewController.finacerId = idArray[indexPath.row]
    }

    self.navigationController?.pushViewController(nextViewController, animated: true)

}
© www.soinside.com 2019 - 2024. All rights reserved.