2016-11-11 82 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