Reputation: 47
i want to assign value to array and show that values in table view.. i am getting the values through webservice using webservice code. the values are getting perfectly, i use print statement for testing it shows perfectly that values. but when i assign that values to table view it shows nothing. here is my code. please help me out
let con = "http://192.168.10.10/Hand.svc/";
var list = [String]()
var answer:String = ""
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: con+"[email protected]")
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil
{
print("Error")
}
else
{
if let content = data
{
do
{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
let asd = myJson.value(forKey: "getlocationResult")
self.answer = String(describing: asd!)
var mystring = self.answer.components(separatedBy:"=")
let size = mystring.count
var i = 0
while (i < size-1 )
{
let st1 = mystring[i+1]
self.list = st1.components(separatedBy: ";")
print (self.list[0])
i += 1
}
}
catch
{
}
}
}
}
task.resume()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return(list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
}
Upvotes: 0
Views: 192
Reputation: 10610
DispatchQueue.main.async
- for update the UI why because network call is background queue .
do {
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
let asd = myJson.value(forKey: "getlocationResult")
self.answer = String(describing: asd!)
var mystring = self.answer.components(separatedBy:"=")
let size = mystring.count
var i = 0
while (i < size-1 )
{
let st1 = mystring[i+1]
self.list = st1.components(separatedBy: ";")
print (self.list[0])
i += 1
}
DispatchQueue.main.async(execute: { () -> Void in
self.yourTableViewName.reloadData()
})
}
catch
{
}
vadian comment ;
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return list.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
}
Upvotes: 1