如何根据 swift 中的 Json id 从 collection 视图 didSelectItemAt 推送不同的视图控制器
How to push different view controller from collection view didSelectItemAt based on Json id in swift
我的项目包含 collectionView.. 但是如何从基于 json id 的 didSelectItemAt 推送不同的 viewcontroller.. 我为每个 id 设置了单独的 vewcontroller.. 但我无法推送不同的基于 json id.
的带有 didSelectItemAt 的视图控件
这是我的 Json 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
print("home financer id \(id)")
self.idArray.append(id)
print("the home financer idsArray \(self.idArray.append(id))")
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。任何人都请在这里帮助我。
更新您的 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()
}
在您的 MakePaymentViewController
中创建一个名为 financerId
的字符串 属性
在你的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)
}
}
更新
for financer in financerArray {
if let id = financer["id"] as? Int {
self.idArray.append(id)
}
if let pic = financer["icon"] as? String {
elf.itemsArray.append(JsonData(icon: pic))
}
}
我的项目包含 collectionView.. 但是如何从基于 json id 的 didSelectItemAt 推送不同的 viewcontroller.. 我为每个 id 设置了单独的 vewcontroller.. 但我无法推送不同的基于 json id.
的带有 didSelectItemAt 的视图控件这是我的 Json 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
print("home financer id \(id)")
self.idArray.append(id)
print("the home financer idsArray \(self.idArray.append(id))")
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。任何人都请在这里帮助我。
更新您的 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()
}
在您的 MakePaymentViewController
financerId
的字符串 属性
在你的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)
}
}
更新
for financer in financerArray {
if let id = financer["id"] as? Int {
self.idArray.append(id)
}
if let pic = financer["icon"] as? String {
elf.itemsArray.append(JsonData(icon: pic))
}
}