Answer the question
In order to leave comments, you need to log in
I don't even know how to describe the question! But the essence of sorting an array or replacing it with a non-array???
the essence is that there is an array of the names of the posts, and there is an array of the IDs of these posts and they match in number and number in the array.
When a person clicks on a cell, he goes to the post. BUT!!!! Implemented word search that filters the array of post titles, but during filtering, the array of IDs no longer matches the array of filtered post titles.
HOW to filter them at the same time or can use not an array but a dictionary. In short, I don't even know what to do. Send!!!
class TableViewController: UITableViewController, UISearchResultsUpdating {
var searchController : UISearchController!
var resultController = UITableViewController()
var arFiltLet = [String]()
var arWords = [String]()
var arID = [String]()
func downloadData(){
let uri = "http://urlurlurl"
let url = NSURL(string: uri);
let request = NSMutableURLRequest(url: url as! URL)
let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in
guard error == nil && data != nil else {
print("Error:",error)
return
}
let httpStatus = response as? HTTPURLResponse
if httpStatus!.statusCode == 200 {
if data?.count != 0 {
let responseString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! NSDictionary
if let posts = responseString["list"] as? [AnyObject]{
for post in posts{
self.arWords.append(post["NAME"] as! String)
self.arID.append(post["ID"] as! String)
}
}
else {
print("I could not find list array")
}
DispatchQueue.main.sync {
self.tableView.reloadData()
}
}
else{
print("No data got from url!")
}
}
else{
print("error httpstatus code is :",httpStatus!.statusCode)
}
}
task.resume()
}
override func viewDidLoad() {
super.viewDidLoad()
self.downloadData()
self.searchController = UISearchController(searchResultsController: self.resultController)
self.tableView.tableHeaderView = self.searchController.searchBar
self.searchController.searchResultsUpdater = self
self.resultController.tableView.dataSource = self
self.resultController.tableView.delegate = self
definesPresentationContext = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func updateSearchResults(for searchController: UISearchController) {
self.arFiltLet = self.arWords.filter{(lett : String) -> Bool in
if lett.lowercased().contains(self.searchController.searchBar.text!.lowercased()){
return true
}else{
return false
}
}
self.resultController.tableView.reloadData()
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.tableView {
return self.arWords.count
}else{
return self.arFiltLet.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if tableView == self.tableView{
cell.textLabel?.text = self.arWords[indexPath.row]
}else{
cell.textLabel?.text = self.arFiltLet[indexPath.row]
}
return cell
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueTableToalpavitTable" && searchController.isActive {
if let indexPath = self.resultController.tableView.indexPathForSelectedRow {
let destanation = segue.destination as! DetailViewController
print(indexPath.row)
destanation.idPost = arID[(indexPath.row)]
} else if let indexPath = self.tableView.indexPathForSelectedRow {
let destanation = segue.destination as! DetailViewController
destanation.idPost = arID[(indexPath.row)]
}
}
if segue.identifier == "segueTableToalpavitTable" {
if let indexPath = self.tableView.indexPathForSelectedRow {
let destanation = segue.destination as! DetailViewController
destanation.idPost = arID[(indexPath.row)]
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question