我使用viewController创建一个搜索栏,在viewController中有一个tableview单元格,其中有两个标签名为:countryName和number。我还把uisearchbar放在viewController中。
在这个问题上,我只能搜索不是countryName的数字,
如何搜索数字和countryNames?
我的代码:
模型
import Foundation
import UIKit
struct ModelData {
var countryName: String
var numbers: String
}
dateBeliVitzel
class DataTableViewCell: UITableViewCell {
@IBOutlet weak var numbers: UILabel!
@IBOutlet weak var countryName: UILabel!
}
视图控制器
import UIKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var countrySearch: UISearchBar!
var searchedCountry = [ModelData]()
var searching = false
var dataJson = [modelData]()
override func viewDidLoad() {
super.viewDidLoad()
countrySearch.delegate = self
fetchData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching{
return searchedCountry.count
} else{
return dataJson.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:"DataTableViewCell", for: indexPath) as! DataTableViewCell
if searching {
cell?.numbers.text = searchedCountry[indexPath.row]
cell?.countryName.text = searchedCountry[indexPath.row]
} else {
cell?.numbers.text = dataJson[indexPath.row]
cell?.countryName.text = dataJson[indexPath.row]
}
return cell
}
}
从url调用json:
func fetchData(){
DispatchQueue.main.async {
let url = ""
Alamofire.request(url, method: .get, encoding: URLEncoding.default).responseJSON{
(response) in
switch response.result{
case .success(let value):
let json = JSON(value)
if let data = json["data"].array{
for item in data{
self.tableView.reloadData()
let numbers = item["numbers"].stringValue
let countryName = item["countryName"].string
let data = ModelData(countryName: countryName, numbers: numbers)
self.dataJson.append(data)
}
}
case .failure(let error):
print(error)
}
}
}
}
这是扩展viewController:
extension ViewController: UISearchBarDelegate{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchedCountry = dataJson.filter({$0.numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry = dataJson.filter({$0.countryName.lowercased().prefix(searchText.count) == searchText.lowercased()})
searching = true
self.tableView.reloadData()
}
}
在你的searchBar
函数中,searchedCountry
只会搜索countryName
:
//This filter will be replaced by the next filter
searchedCountry = dataJson.filter({$0.numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry = dataJson.filter({$0.countryName.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry
设置两次,numbers
过滤器将被countryName
取代
您将不得不搜索多个选项,如下所示:
searchedCountry = dataJson.filter({$0.countryName.lowercased().prefix(searchText.count) == searchText.lowercased() || $0.numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})