2017-11-30 14 views
0

phpからJSON配列を受け取り、データをテーブルビューに渡そうとしています。ただし、私のtableviewはデータを表示しません。Swift 3:カスタムセルにデータが表示されない

class test1ViewController: UIViewController , UITableViewDataSource, UITableViewDelegate { 


var TableData:Array<String> = Array <String>() 



public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
{ 

    return TableData.count 

} 

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 


    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! testTableViewCell 

    cell.mylabel1.text = TableData[indexPath.row] 
    return cell 

} 

func get_data_from_url(_ link:String) 
{ 
    let url:URL = URL(string: link)! 
    let session = URLSession.shared 

    let request = NSMutableURLRequest(url: url) 
    request.httpMethod = "GET" 
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData 


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

     guard let _:Data = data, let _:URLResponse = response , error == nil else { 

      return 
     } 

     print(data!) 
     self.extract_json(data!) 


    }) 

    task.resume() 

} 

func extract_json(_ data: Data) 
{ 


    let json: Any? 

    do 
    { 
     json = try JSONSerialization.jsonObject(with: data, options: []) 
     // print(json!) 

    } 
    catch 
    { 
     return 
    } 

    guard let data_list = json as? NSArray else 
    { 
     return 


    } 

    if let countries_list = json as? NSArray 
    { 
     for i in 0 ..< data_list.count 
     { 
      if let country_obj = countries_list[i] as? NSDictionary 
      { 

       if let name = country_obj["name"] as? String 
       { 
        if let age = country_obj["age"] as? String 
        { 
         TableData.append(name + age) 




        } 
       } 
      } 
     } 
    } 




} 

配列が 動物のような初期値を与えた場合= ["hinno", "ali", "khalil"]、 値はカスタムセルに表示されますが、私は、サーバーからデータを取得し、JSON形式の変換を行うときに、何も表示されません。

答えて

0

リロードコレクションのデータまたはのtableViewこの

DispatchQueue.main.async { 
     yourTableViewName.reloadData() 
} 
1

tableview.reloadData()配列に変更を加えるといつでも。

+0

などの派遣コードにはあまりデータが遂に登場いただき、ありがとうございますことを使用することを動作しない場合 –

関連する問題