2017-03-16 43 views
0

我是Swift的新手,正在尝试创建一个从我的网站读取JSON数据的表。我跟着一些教程,并能够让我的代码与表控制器一起工作。现在我试图在视图控制器中使用一个表视图,所以我可以有更多的定制。我的问题是,当我尝试使用我的新代码时,无法真正显示数据。从外部服务器连接JSON到Swift tableView

这是我在我的viewController.swift:

import UIKit 

class ViewController: UIViewController { 

@IBOutlet weak var tableView: UITableView! 
var TableData:Array<String> = Array <String>() 


override func viewDidLoad() { 
    super.viewDidLoad() 

    get_data_from_url("http://www.stevenbunting.org/Alliris/service.php") 
} 



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

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


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 

    cell.textLabel?.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 
     } 


     self.extract_json(data!) 


    }) 

    task.resume() 

} 


func extract_json(_ data: Data) 
{ 


    let json: Any? 

    do 
    { 
     json = try JSONSerialization.jsonObject(with: data, options: []) 
    } 
    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 country_name = country_obj["user"] as? String 
       { 
        if let country_code = country_obj["friendlist"] as? String 
        { 
         TableData.append(country_name + " [" + country_code + "]") 
        } 
       } 
      } 
     } 
    } 



    DispatchQueue.main.async(execute: {self.do_table_refresh() 

    }) 

} 

func do_table_refresh() 
{ 
    self.tableView.reloadData() 

} 


} 
+0

你是否将ViewController设置为tableView的数据源?可能不会,因为你的ViewController代码没有实现[UITableViewDataSource-protocol](https://developer.apple.com/reference/uikit/uitableviewdatasource);) –

回答

0

也许你没有设置的tableView的数据源。要做到这一点,实现在视图控制器类的UITableViewDataSource-protocol并在viewDidLoad()中的tableView的DataSource属性设置为self,例如:

class ViewController: UIViewController, UITableViewDataSource { 
// ... 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     tableView.dataSource = self 
     // ... 
    } 
    //... 
} 

哦,不要忘了苹果的交通运输安全的设置,否则你不会看到任何东西,因为iOS不再允许HTTP,你已经使用HTTPS。处理此问题的正确方法是为您的域获取SSL证书。

quick'n'dirty和绝对不推荐的方法是disable ATS or to set an exception for certain, trustworthy domains