2016-11-11 9 views
0

AlamofireSwiftyJsonを使用してAPIに接続しました。APIからのJSONデータでTableViewが表示されない

私は、このJSONリターンを得た:

{ 
"contacts": [ 
     { 
       "id": "c200", 
       "name": "Ravi Tamada", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 
     { 
       "id": "c201", 
       "name": "Johnny Depp", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 
     { 
       "id": "c202", 
       "name": "Leonardo Dicaprio", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     } 
] 
} 

これは私のViewControllerコードです:

 
import UIKit 
import Alamofire 
import SwiftyJSON 

class ViewController:UIViewController { 
    @IBOutlet weak var tblJSON: UITableView! 

    var arrRes = [[String : AnyObject]]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     getRequest() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func getRequest() { 
     Alamofire.request("http://api.androidhive.info/contacts").responseJSON { (responseData) -> Void in 
      if ((responseData.result.value) != nil) { 
       let swiftyJsonVar = JSON(responseData.result.value!) 

       if let resData = swiftyJsonVar["contacts"].arrayObject { 
        self.arrRes = resData as! [[String : AnyObject]] 
       } 

       if self.arrRes.count > 0 { 
        self.tblJSON.reloadData() 
       } 
      } 
     } 
    } 

    //MARK: tableView 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return arrRes.count 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell : UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell")! 
     var dict = arrRes[indexPath.row] 
     cell.textLabel?.text = dict["name"] as? String 
     cell.detailTextLabel?.text = dict["email"] as? String 
     return cell 
    } 

} 


私はトラブルUITableViewに結果を表示しています。 誰でも助けてくれますか?ありがとう!

+0

'numberOfRowsInSection'' cellForRowAt'のようなあなたのtableViewにDataSourceDelegateを割り当てる必要があり、この方法です呼び出される? – Rajat

+0

1. 'ViewController'をストーリーボードのtableViewのデリゲートとデータソースとして設定しましたか? 2.オーバーライド 'numberOfSectionsInTableView:'を1に設定しましたか? –

+0

メインキューで 'reloadData'を呼び出す必要があります。 – rmaddy

答えて

3

あなたはこの

class ViewController:UIViewController, UITableViewDataSource, UITableViewDelegate 

ようUITableViewDataSourceUITableViewDelegateを実装する必要があるとviewDidLoadに、あなたはこの

override func viewDidLoad() { 
    super.viewDidLoad() 
    tblJSON.dataSource = self 
    tblJSON.delegate = self 
    getRequest() 
} 
+0

ありがとうございました!できます! –

+0

多分あなたが知っている、なぜ私はコードで書くが、 "名前"だけを表示するTableView:cell.textLabel?.text = dict ["name"] as?文字列; cell.detailTextLabel?.text = dict ["email"] as?文字列 –

+0

'print(cell.detailTextLabel)' cellForRowにこの行を追加し、それがnilでないかどうかを確認します。 – Rajat

関連する問題