2017-08-08 1 views
0

私はこれらの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" を選択します

  1. 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 
        } 
    
    } 
    

    です。

  2. JSON "name"をUITableViewに表示します。 UILabel nameLbl
  3. と表示JSON "ID" UILabel idNumberLblとJSONに"名前"

私は、pickerViewを使用するときにstructを使用してjson値を別々の出力に分割することができますが、テーブルビューを使用するときは関連付けることはできません。

How to display JSON into UILabel using UIPickerView in Swift 3.0?

誰かが助けることができますか?ありがとう。

答えて

0

あなたの配列でyourJsonArrayを交換し、いくつかの変更で、私はあなたのコードの過去

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! 

    var yourJsonArray = NSArray() 


    // 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 yourJsonArray.count 
    } 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

     cell.textLabel?.text = (yourJsonArray.object(at: indexPath.row) as! NSDictionary).object(forKey: "name") as? String 
     return cell 
    } 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     //let cell = tableView.cellForRow(at: indexPath) 
     let title = (yourJsonArray.object(at: indexPath.row) as! NSDictionary).object(forKey: "name") as? String 

     selectBtn.setTitle(title, for: .normal) 
     self.tableview.isHidden = true 
    } 

    @IBAction func selectName(_ sender: Any) { 
     self.tableview.isHidden = !self.tableview.isHidden 
    } 

} 
+0

tq。私はまずそれを試してみる – Rozaimi

+0

それは動作しませんでした。 。 。 UItableviewに構造体を適用する方法のコードを更新できますか? – Rozaimi

0

あなたのアレイを使用してJSON配列を元に戻し、SwiftyJSON

Uが使用でき
import UIKit 
import SwiftyJSON 

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 data = UserModel(array[indexPath.row]) 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    cell.textLabel?.text = data.title // get id by data.id 
    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 
} 

}

を使用してstructSwiftyJSON

フィードバックのためには、
import Foundation 
import SwiftyJSON 

struct UserModel { 
    var name:String? 
    var id:String? 
    init(json:JSON) { 
     name = "\(json["name"])" 
     id = "\(json["id"])" 
    } 
} 
+0

提案していただきありがとうございます。 :)。 。 – Rozaimi