2016-11-11 54 views
0

我使用AlamofireSwiftyJson連接到API。TableView不顯示來自API調用的JSON數據的文本

我得到這個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'這種方法被叫? – Rajat

+0

1.您是否將'ViewController'設置爲storyboard中tableView的委託和數據源? 2.你設置了重寫'numberOfSectionsInTableView:'返回1嗎? –

+0

您必須在主隊列上調用'reloadData'。 – rmaddy

回答

3

您需要實現UITableViewDataSourceUITableViewDelegate這樣

class ViewController:UIViewController, UITableViewDataSource, UITableViewDelegate 

viewDidLoad需要分配DataSourceDelegate您的tableView這樣

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中添加此行並檢查它是否爲零 – Rajat