I copy the entire code first, and then go over it

class Regions: UITableViewController { let arr = ["ddd","sss"] override func viewDidLoad() { super.viewDidLoad() tableView.register(UITableViewCell.self, forCellReuseIdentifier: "myCell") ViewHelper.WaitingIndicator.indicatorStart(vc: self, view: view.self) Alamofire.request("http://app.com.ua/reference/regions").validate().responseJSON { response in switch response.result { case .success(let value): let json = JSON(value) let success = json["success"].stringValue print(success) var regions = [String]() var ids = [String]() for (_,subJson):(String, JSON) in json["data"] { let id = subJson["id"].stringValue let name = subJson["name"].stringValue regions.append(name) ids.append(id) } print(regions) print(ids) ViewHelper.WaitingIndicator.indicatorStop() case .failure(let error): print(error) ViewHelper.WaitingIndicator.indicatorStop() } } } override func numberOfSections(in tableView: UITableView) -> Int { return 1 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arr.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) cell.textLabel?.text = arr[indexPath.row] return cell } 

As you can see, for the test, I set the information from the topmost array of let arr = ["ddd","sss"] . -

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arr.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) cell.textLabel?.text = arr[indexPath.row] return cell } 

How to set information from the newly formed regions array from here

 var regions = [String]() var ids = [String]() for (_,subJson):(String, JSON) in json["data"] { let id = subJson["id"].stringValue let name = subJson["name"].stringValue regions.append(name) ids.append(id) } 

That is, refer to it. When you create at the very top, in place of let arr = ["ddd","sss"] get an appeal to an empty, not yet filled with Json array, and in

 `viewDidLoad()` 

you will not turn, and static too you will not make In general, I can not catch up with this elementary)

  • one
    A little scary, but first, to make it clearer by example, make your arr variable: var arr = ["ddd","sss"] . In the query, make self.arr = regions and self.tableView.reloadData() . A list of regions received should be displayed. - VAndrJ
  • You mentioned in your question that there must be another class. However, in the code you give it is only one. Did you remember to add code from another class? Or am I misunderstanding something? - Roman Podymov
  • @ VAndrJ Well, wow, this is how to pervert) Thanks, it works, I understood the mechanism. The android is easier) - Romik romikromik
  • @Roman Podymov did not formulate correctly, I agree. I will delete this part - Romik romikromik
  • @Romikromikromik wouldn’t say if I’m going to understand it - much better done in iOS (purely subjective opinion) - VAndrJ

1 answer 1

You display information in the UITableView , where in the UITableViewDataSource give data from your arr

 var arr = ["ddd","sss"] 

Then you get the data and in closure you need to add / replace the data and then reload the table:

 self.arr = regions self.tableView.reloadData() 

And the table will display the updated data.