Reputation: 1177
I have parsed a JSON using Alamofire and SwiftyJSON to get a list of the names of cryptocurrencies and populate a table view. I then am trying to filter the results using a UISearchBar, but whatever I search, it always shows the first result of the array of names, even though the filter is working as I tested it using print and it correctly filters. for example, i search for Ethereum and the console prints ["name": "Ethereum"] but the Tableview will show "Bitcoin" always, if i search a name that doesn't exist, it shows nothing. Can anyone identify what is wrong in my code that causes this problem? Thank you very much, I am new to any kind of coding.
here is my code:
//
// StartViewController.swift
// CryptoClockTesting
//
// Created by Peter Ruppert on 08/07/2018.
// Copyright © 2018 Peter Ruppert. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
class StartViewController: UIViewController, UISearchBarDelegate,
UITableViewDelegate, UITableViewDataSource {
var names = [[String:String]]()
var isSearching = false
var filteredData = [[String:String]]()
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = names[indexPath.row]
let text: [String:String]
if isSearching {
text = filteredData[indexPath.row]
print(text)
} else {
text = name
}
cell.textLabel?.text = name["name"]
cell.textLabel?.textColor = UIColor.blue
return cell
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if searchBar.text == nil || searchBar.text == "" {
isSearching = false
view.endEditing(true)
tableView.reloadData()
} else {
isSearching = true
filteredData = names.filter({
$0["name"] == searchBar.text
})
tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching {
return filteredData.count
} else {
return names.count
}
}
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
tableView.isHidden = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
tableView.isHidden = true
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.isHidden = true
searchBar.delegate = self
tableView.delegate = self
tableView.dataSource = self
searchBar.returnKeyType = UIReturnKeyType.done
self.view.backgroundColor = .blue
let urlString = "https://api.coingecko.com/api/v3/coins"
if let url = URL(string: urlString) {
if let data = try? String(contentsOf: url) {
let json = JSON(parseJSON: data)
parse(json: json)
}
}
}
//Parsing News API and adding to table, then use table view setup to properly display.
func parse(json: JSON) {
for result in json[].arrayValue {
let name = result["name"].stringValue
let obj = ["name": name]
names.append(obj)
}
tableView.reloadData()
}
}
Upvotes: 0
Views: 48
Reputation: 100503
This because you always set it to name["name"]
in cellForRowAt
if isSearching {
text = filteredData[indexPath.row]
print(text)
} else {
text = name
}
cell.textLabel?.text = name["name"] // should be text["name"]
Upvotes: 1