0

I am trying to display some data I take from a data base into a TableView but the data is not shown in the TableView. The data I receive is formatted in JSON.

This is the data I receive form the data base and what I want to print in the TableViewis just David:

{"name":"David"}

This is the code to get the data from de data base:

let session = NSURLSession.sharedSession()
    let request = NSURLRequest(URL: NSURL(string: "http://localhost:8888/Patients.php")!)
    let task = session.dataTaskWithRequest(request) {data, response, downloadError in

        if let error = downloadError {
            print("Could not complete the request \(error)")
        }
        else {

            do {
                self.json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
            } catch {
                fatalError()
            }
            dispatch_async(dispatch_get_main_queue(), {

                if let parseJSON = self.json{

                    let name = parseJSON["name"] as! String

                    self.arrayData.append(name)

                    print("Data1:\(self.arrayData)")
                }

            })
        }
    }
    task.resume()

arrayData is an array where I put the data I receive from the data base and it is declared like this:

var arrayData: [String] = []

The code

print("Data1:\(self.arrayData)")

show in the console this Data1:["David"], so I get the data correctly.

Then I implement the methods to print in the ´TableView´, the numberOfSectionsInTableViewmethod, the numberOfRowsInSection method and the cellForRowAtIndexPath method but the data is not printed in the TableView.

I think the problem is that the TableViewis drawn before I put the data in the array so it prints nothing because the array is empty, and I don´t know how to fix it.

Anyone knows what is my problem?

2
  • 2
    Have you tried to call tableView.reloadData() after changing contents of the array? Commented Dec 1, 2015 at 10:21
  • 1
    That solved my problem! Thank you Sebastian! Commented Dec 1, 2015 at 10:54

1 Answer 1

1

yes, you're right.

session.dataTaskWithRequest

is async. Data is not returned immediately, it have delay. You must to reload tableview after recieved data:

self.arrayData.append(name)
self.tableview.reloadData()

Usually i will use block:

func getData(handleComplete:((arr:NSMutableArray)->())){
    let aray = NSMutableArray()
    let session = NSURLSession.sharedSession()
    let request = NSURLRequest(URL: NSURL(string: "http://localhost:8888/Patients.php")!)
    let task = session.dataTaskWithRequest(request) {data, response, downloadError in

        if let error = downloadError {
            print("Could not complete the request \(error)")
        }
        else {

            do {
                self.json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments)
            } catch {
                fatalError()
            }
            dispatch_async(dispatch_get_main_queue(), {

                if let parseJSON = self.json{

                    let name = parseJSON["name"] as! String

                    aray.append(name)

                    print("Data1:\(self.arrayData)")
                }
                handleComplete(aray)

            })
        }
    }
    task.resume()
    arrayData
}

and in viewdidload i will call:

override func viewDidLoad() {
    super.viewDidLoad()
    self.getData { (arr) -> () in
        self.tableview.reloadData()
    }
}

it 's better

Sign up to request clarification or add additional context in comments.

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.