使用Swift在TableView中显示JSON数据 [英] Show JSON data in a TableView with Swift
问题描述
我正在尝试显示从数据库中获取的一些数据到TableView
中,但该数据未显示在TableView
中.我收到的数据采用JSON格式.
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.
这是我从数据库收到的数据,我想在TableView
中打印的只是David:
This is the data I receive form the data base and what I want to print in the TableView
is 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是一个array
,我将从数据库中接收的数据放入其中,并声明为:
arrayData is an array
where I put the data I receive from the data base and it is declared like this:
var arrayData: [String] = []
代码
print("Data1:\(self.arrayData)")
在控制台中显示此Data1:["David"]
,所以我可以正确获取数据.
show in the console this Data1:["David"]
, so I get the data correctly.
然后,我实现了要在"TableView",numberOfSectionsInTableView
方法,numberOfRowsInSection
方法和cellForRowAtIndexPath
方法中打印的方法,但是数据没有在TableView
中打印.
Then I implement the methods to print in the ´TableView´, the numberOfSectionsInTableView
method, the numberOfRowsInSection
method and the cellForRowAtIndexPath
method but the data is not printed in the TableView
.
我认为问题在于TableView
是在将数据放入array
之前绘制的,因此它不打印任何内容,因为数组为空,而且我不知道如何解决它.
I think the problem is that the TableView
is 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?
推荐答案
是的,您是对的.
session.dataTaskWithRequest
session.dataTaskWithRequest
是异步的.数据不会立即返回,它会延迟. 您必须在收到数据后重新加载tableview:
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()
通常,我将使用块:
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
}
在viewdidload中,我将调用:
and in viewdidload i will call:
override func viewDidLoad() {
super.viewDidLoad()
self.getData { (arr) -> () in
self.tableview.reloadData()
}
}
更好
这篇关于使用Swift在TableView中显示JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!