So I have two UITableViews in one ViewController, that are meant to be updated depending on the user's UIPickerView selection. I have two functions which call an API, and display the results in the tables.I want the table views to be updated, but currently, they new data is added to bottom of the table. My question is how do I update the table? I've tried calling reloadTable() on them, but nothing.
This is the function that calls the API, and displays the data
func fetchDataW() { print("This is the Sprints rank function\n") let url = URL(string: Constants.rankingAPI) guard let requestUrl = url else { fatalError() } var request = URLRequest(url: requestUrl) request.httpMethod = "POST" let group = DispatchGroup() group.enter() request.httpBody = u20VC.paramW.description.data(using: String.Encoding.utf8) let task = URLSession.shared.dataTask(with: request) { [self] (data, response, error) in // Check for Error if let error = error { print("Error took place \(error)") return } // Convert HTTP Response Data to a String if let data = data, var dataString = String(data: data, encoding: .utf8) { do{ dataString = "<table>" + dataString let docu = HTMLDocument(string: dataString) let elements = docu.querySelectorAll("td") let first5 = Array(elements.prefix(55)) let first = Rankings(rank: first5[0].textContent, mark: first5[1].textContent, wind: first5[3].textContent, name: first5[4].textContent, club: first5[6].textContent, yob: first5[7].textContent, plc: first5[8].textContent, location: first5[9].textContent, date: first5[10].textContent) DispatchQueue.main.async { tblWomen.dataSource = self tblWomen.delegate = self tblWomen.reloadData() } listW.append(first) listW.append(second) listW.append(third) listW.append(fourth) listW.append(fifth) } } } task.resume() group.leave() }
And here is my case statement for the PickerView.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { switch row { case 0: u20VC.paramM = Parameters.u20_100mParamsM u20VC.paramW = Parameters.u20_100mParamsM self.tblMen.reloadData() self.tblWomen.reloadData() self.fetchDataM() self.fetchDataW() default: ...}
Each time a PickerView item is selected, the value of u20VC.paramM and u20VC.paramW are changed to the correct HTTPBody parameters, and the functions that make the API call(fetchDataM() and fetchDataW() ) are called.
The DataSource is in the function running on the main thread.
Where do I call the reloadData() function?