[Solved] Populating a UITable with NSDictionary in SWIFT


Here’s what you’ll need to do:

First, create a class variable for your data. Put this under your class declaration:

var dataDict: [String:AnyObject]?

Then, to keep things organized, put your tableView property and your userDetails property directly under the dataDict.

@IBOutlet var tableView: UITableView!
let userDetails: [String] = UserDefaults.standard.stringArray(forKey:"UserDetailsArray")

Now, your viewDidLoad method should look like this:

override func viewDidLoad() {
    super.viewDidLoad()
    self.dataDict = [String:AnyObject]()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.getSubjects()
}

Below that, you’ll want to take care of the UITableViewDataSource methods:

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.dataDict.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
    if let array = self.dataDict[String(indexPath.row)] as? [String] {
        cell.textLabel?.text = array[0]
        cell.detailTextLabel?.text = array[1]
    }
    return cell
}

Lastly, you need to implement the getSubjects() method:

func getSubjects() {

    let myUrl = NSURL(string: "www.mydomain.com/retrieveSubjects.php");
    let request = NSMutableURLRequest(url:myUrl as! URL)
    let user_id = UserDetails[0]
    request.httpMethod = "POST";
    let postString = "user_id=\(user_id)";
    request.httpBody = postString.data(using: String.Encoding.utf8);

    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        var err: NSError?
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            if let parseJSON = json {
                let resultValue = parseJSON["subjects"] as! [String:AnyObject]
                self.dataDict = resultValue
                self.tableView.reloadData()
            }
        } catch let error as NSError {
            err = error
            print(err!);
        }
    }
    task.resume();
}

Altogether, your code should look like this:

class SubjectsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var dataDict: [String:AnyObject]?
    @IBOutlet var tableView: UITableView!
    let userDetails: [String] = UserDefaults.standard.stringArray(forKey:"UserDetailsArray")

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataDict = [String:AnyObject]()
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.getSubjects()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataDict.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        if let array = self.dataDict[String(indexPath.row)] as? [String] {
            cell.textLabel?.text = array[0]
            cell.detailTextLabel?.text = array[1]
        }
        return cell
    }

    func getSubjects() {

        let myUrl = NSURL(string: "www.mydomain.com/retrieveSubjects.php");
        let request = NSMutableURLRequest(url:myUrl as! URL)
        let user_id = UserDetails[0]
        request.httpMethod = "POST";
        let postString = "user_id=\(user_id)";
        request.httpBody = postString.data(using: String.Encoding.utf8);

        let task = URLSession.shared.dataTask(with: request as URLRequest) {
            data, response, error in

            if error != nil {
                print("error=\(error)")
                return
            }

            var err: NSError?
            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
                if let parseJSON = json {
                    let resultValue = parseJSON["subjects"] as! [String:AnyObject]
                    self.dataDict = resultValue
                    self.tableView.reloadData()
                }
            } catch let error as NSError {
                err = error
                print(err!);
            }
        }
        task.resume();
    }
}

14

solved Populating a UITable with NSDictionary in SWIFT