如何使用 swift 在 table 视图中搜索多个数据?
how to search multiple data in table view using swift?
我使用 viewController 创建了一个搜索栏,在 viewController 中有一个带有 2 个标签的表格视图单元格,名称为:countryName 和 number。我也把 uisearchbar 放在 viewController.
在这个问题中我只能搜索数字而不是国家名称,
如何搜索号码和国家/地区名称?
这里是我的代码:
型号
import Foundation
import UIKit
struct ModelData {
var countryName: String
var numbers: String
}
DataTableViewCell
class DataTableViewCell: UITableViewCell {
@IBOutlet weak var numbers: UILabel!
@IBOutlet weak var countryName: UILabel!
}
ViewController
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({[=16=].numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry = dataJson.filter({[=16=].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({[=10=].numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry = dataJson.filter({[=10=].countryName.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry
设置了两次,numbers
过滤器将被 countryName
替换
您必须像这样搜索多个选项:
searchedCountry = dataJson.filter({[=11=].countryName.lowercased().prefix(searchText.count) == searchText.lowercased() || [=11=].numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
我使用 viewController 创建了一个搜索栏,在 viewController 中有一个带有 2 个标签的表格视图单元格,名称为:countryName 和 number。我也把 uisearchbar 放在 viewController.
在这个问题中我只能搜索数字而不是国家名称,
如何搜索号码和国家/地区名称?
这里是我的代码:
型号
import Foundation
import UIKit
struct ModelData {
var countryName: String
var numbers: String
}
DataTableViewCell
class DataTableViewCell: UITableViewCell {
@IBOutlet weak var numbers: UILabel!
@IBOutlet weak var countryName: UILabel!
}
ViewController
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({[=16=].numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry = dataJson.filter({[=16=].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({[=10=].numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry = dataJson.filter({[=10=].countryName.lowercased().prefix(searchText.count) == searchText.lowercased()})
searchedCountry
设置了两次,numbers
过滤器将被 countryName
您必须像这样搜索多个选项:
searchedCountry = dataJson.filter({[=11=].countryName.lowercased().prefix(searchText.count) == searchText.lowercased() || [=11=].numbers.lowercased().prefix(searchText.count) == searchText.lowercased()})