私はこれらのJSON値にID、名前、電子メールが含まれています。UItableViewにJSON値を表示し、その値をSwift 3.0のUILabelに分割する方法は?
[
{"id":"101", "name":"Tony", "email":"[email protected]"},
{"id":"102", "name":"Billy", "email":"[email protected]"}
]
はこれまでのところ、スウィフト3.0で、私はUIButtonに手動でのUITableViewに表示出力とディスプレイ出力に管理されます。のみ上記のJSON値に私は"idとname" を選択します
- :
ViewController.swift
私が達成したい何import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // Outlet @IBOutlet var deptLbl: UILabel! @IBOutlet var selectBtn: UIButton! @IBOutlet var tableview: UITableView! @IBOutlet var idNumberLbl: UILabel! @IBOutlet var nameLbl: UILabel! // Variable let array = ["Billy","Dale","Sheila","Thomas","Tony"] override func viewDidLoad() { super.viewDidLoad() self.tableview.isHidden = true } // Table func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return array.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) cell.textLabel?.text = array[indexPath.row] return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) selectBtn.setTitle(cell?.textLabel?.text, for: .normal) self.tableview.isHidden = true } @IBAction func selectName(_ sender: Any) { self.tableview.isHidden = !self.tableview.isHidden } }
です。
- JSON "name"をUITableViewに表示します。 UILabel nameLblへ
- と表示JSON "ID" UILabel idNumberLblとJSONに"名前"。
私は、pickerViewを使用するときにstructを使用してjson値を別々の出力に分割することができますが、テーブルビューを使用するときは関連付けることはできません。
How to display JSON into UILabel using UIPickerView in Swift 3.0?
誰かが助けることができますか?ありがとう。
tq。私はまずそれを試してみる – Rozaimi
それは動作しませんでした。 。 。 UItableviewに構造体を適用する方法のコードを更新できますか? – Rozaimi