21

I connected to API using Alamofire and SwiftyJson.

I got this JSON return:

{
"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"
                }
        }
 ]
}

This is my ViewController code:

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
    }

}


I have trouble displaying result on the UITableView. Can anyone help me? Thanks!

7
  • 1
    numberOfRowsInSection cellForRowAt are this method get called ? Commented Nov 11, 2016 at 18:48
  • 1
    1. Did you set the ViewController as the delegate and datasource of your tableView in the storyboard? 2. Did you set override numberOfSectionsInTableView: returning 1 ? Commented Nov 11, 2016 at 18:50
  • 1
    You must call reloadData on the main queue. Commented Nov 11, 2016 at 18:56
  • @W.K.S There is no need to implement numberOfSectionsInTableView if you only have one section. That is the default. Commented Nov 11, 2016 at 18:57
  • @rmaddy Sorry, I worded that badly: I meant has OP overridden the method and returned 0 because he thought there are no sections. It's a mistake I made when I was new. Commented Nov 11, 2016 at 18:59

1 Answer 1

18

You need to implement UITableViewDataSource and UITableViewDelegate like this

class ViewController:UIViewController, UITableViewDataSource, UITableViewDelegate 

and in viewDidLoad you need to assign DataSource and Delegate to your tableView like this

override func viewDidLoad() {
    super.viewDidLoad()
    tblJSON.dataSource = self
    tblJSON.delegate = self
    getRequest()
}
Sign up to request clarification or add additional context in comments.

6 Comments

maybe you know, why TableView displaying only "name" although I write in code: cell.textLabel?.text = dict["name"] as? String; cell.detailTextLabel?.text = dict["email"] as? String
print(cell.detailTextLabel) add this line in cellForRow and check if its not nil
xCode tell me that it's nil
Check if outlet for that label is connected or not
Outlet don't need. Because I use default label (open var textLabel: UILabel? { get } // default is nil. label will be created if necessary.) My label created but my detailLabel don't created. I don't know why. I don't use UILabel in storyboard.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.