0

I have a JSON Data which I want to get into UITable. The data is dynamic so table should update every time view loads. Can anyone help?

{
data =     (
                {
            id = 102076330;
            name = "Vicky Arora";
        }
    )
}
2
  • are you sure it's the correct JSON format? Commented Aug 7, 2015 at 9:24
  • @Dato'MohammadNurdin This is the actual data. { data = ( { id = 1020763302139; name = "Vicky Arora"; } ); paging = { next = "graph.facebook.com/v2.4/XXXXXX"; }; summary = { "total_count" = 13; }; } Commented Aug 7, 2015 at 9:29

3 Answers 3

1

try this....

When you receive response,get the whole array of dictionary

if let arr = response["data"] as? [[String:String]] {
      YourArray = arr  
      // Define YourArray globally
}  

Then in tableview cell,cellForRowAtIndexPath method

if let name = YourArray[indexpath.row]["name"] as? String{
      label.text = name
}
//Same You can done with id 

And don't forget to set number of rows

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.
    return YourArray.count
}
Sign up to request clarification or add additional context in comments.

4 Comments

Use of unresolved identifier "responseObject"? Do you know what that is?
just show me how did you get response...and I''ll tell you what to do?
I am getting the YourArray correct. [[id: 102076330, name: Vicky Arora]] but when I use this in UITableView cellForRow, I get "fatal error: Array index out of range". if let name = YourArray[indexPath.row]["name"] { cell.detailTextLabel!.text = name }. I tried if let name = YourArray[indexPath.row] - it is still crashing at this line with Array index out of range.
If you still get error....please post whole your viewcontroller code....easy for me to solve
1

Try this one. But this sample i'm using Alamofire and SwitfyJSON. Import it using CocoaPod.

import UIKit
import Alamofire

class TableViewController: UITableViewController{

    var users: [JSON] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        Alamofire.request(.GET, "http://xxxxx/users.json").responseJSON { (request, response, json, error) in
            if json != nil {
                var jsonObj = JSON(json!)
                if let data = jsonObj["data"].arrayValue as [JSON]?{
                    self.users = data
                    self.tableView.reloadData()
                }
            }
        }
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return users.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("UserCell", forIndexPath: indexPath) as! UITableViewCell
        let user = users[indexPath.row]
        if let idLabel = cell.viewWithTag(100) as? UILabel {
            if let id = user["id"].string{
                idLabel.text = id
            }
        }

        if let nameLabel = cell.viewWithTag(101) as? UILabel {
            if let name = user["name"].string{
                nameLabel.text = name
            }
        }

        return cell
    }

}

Comments

0

If you are up to using Core Data, I would suggest using the NSFetchedRequest.

Every time you are getting the data from the server, save it to Core data, and that will automatically update the table view.

Here is a tutorial from Ray Wenderlich

Comments

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.