我有一个从服务器获取数据的json,所以有3种不同的类和一个视图控制器
这是主要课程
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return poi.count
}
//the method returning each cell of the list
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ViewControllerTableViewCell
//getting the hero for the specified position
let hero: poiLista
hero = poi[indexPath.row]
//displaying values
cell.labelName.text = hero.nombre
cell.labelID.text? = "\(hero.id_poi)"
return cell
}
这是我用来列出数据的功能的一部分
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL! as URL)
//setting the method to post
request.httpMethod = "GET"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
//exiting if there is some error
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
//converting resonse to NSDictionary
var teamJSON: NSDictionary!
teamJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
//getting the JSON array teams from the response
let teams: NSArray = teamJSON["teams"] as! NSArray
//looping through all the json objects in the array teams
for i in 0 ..< teams.count{
//getting the data at each index
let poiID: Int = (teams[i] as! NSDictionary)["id_poi"] as! Int
let poiName: String = (teams[i] as! NSDictionary)["nombre"] as! String
self.poi.append(poiLista(
id_poi: poiID,
nombre: poiName
))
//displaying the data
print("id -> ", poiID)
print("name -> ", poiName)
}
//displaying data in tableview
self.tableViewMuseo.reloadData()
} catch {
print(error)
}
}
//executing the task
task.resume()
从json中获取原始的跟踪数据,因此我希望将其显示在表中,但它不起作用
id-> 1 名称->学校