自动完成搜索栏
Autocompleting searchBar
我正在尝试在我的 iOS 应用程序中实现一个搜索栏。我正在从应用程序搜索国家和城市。国家和城市名称来自数据库。我在代码中遇到的问题很少。
首先,我的代码现在成功搜索了城市,但没有搜索国家/地区名称,我也不知道如何搜索国家/地区名称。
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsSearchWord: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsSearchWord {
self.filteredKeys.append(key as! String)
}
}
self.tableView.reloadData()
}
第二个问题是我想获取所选国家和城市的 ids
以及用户在应用程序上选择的内容。为了您的信息,我也从服务器获取 ID
这是我的完整代码
class CountryTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filteredKeys = [String]()
var resultSearchController = UISearchController()
var newTableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredKeys.count
} else {
return dict.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
let key = self.filteredKeys[indexPath.row]
let dictionary = self.dict[key] as! NSDictionary
if
let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString),
let stateName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["State"] as? NSDictionary)!["name"] as? NSString),
let shortName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_name"] as? NSString)
{
cell.stateNameLabel.text = stateName as String
cell.cityNameLabel.text = cityName as String
cell.shortNameLabel.text = shortName as String
}
return cell
}else{
if let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString){
cell.cityNameLabel.text = cityName as String
}
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsSearchWord: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsSearchWord {
self.filteredKeys.append(key as! String)
}
}
self.tableView.reloadData()
}
func getCountriesNamesFromServer(searchWord:String){
let url:String = "http://localhost"
let params = ["keyword":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print("result is\(result)")
self.dict = result
}
}
}
}
字典结构:
{
0 = {
City = {
code = 10409;
"country_id" = 244;
id = 8727;
iso = "ZA-05";
lat = "-32.7833000";
longi = "26.8333000";
name = Alice;
"state_id" = 4379;
};
Country = {
id = 244;
name = "South Africa";
"short_name" = ZA;
};
State = {
"country_id" = 244;
id = 4379;
name = "Eastern Cape";
};
};
您只是在字典中搜索 ["City"]
键,应该也添加 ["Country"]
键。
for (key, value) in self.dict {
let valueContainsCity: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
let valueContainsCountry: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsCity || valueContainsCountry{ self.filteredKeys.append(key as! String) }
}
uppercaseString - 您的城市和国家名称是大写字符串吗?如果不做 lowerCaseString
这将为您获取城市 ID 和国家 ID。
(((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["id"] as?NSString)
(((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["id"] as?NSString)
我正在尝试在我的 iOS 应用程序中实现一个搜索栏。我正在从应用程序搜索国家和城市。国家和城市名称来自数据库。我在代码中遇到的问题很少。
首先,我的代码现在成功搜索了城市,但没有搜索国家/地区名称,我也不知道如何搜索国家/地区名称。
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsSearchWord: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsSearchWord {
self.filteredKeys.append(key as! String)
}
}
self.tableView.reloadData()
}
第二个问题是我想获取所选国家和城市的 ids
以及用户在应用程序上选择的内容。为了您的信息,我也从服务器获取 ID
这是我的完整代码
class CountryTableViewController: UITableViewController, UISearchResultsUpdating {
var dict = NSDictionary()
var filteredKeys = [String]()
var resultSearchController = UISearchController()
var newTableData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
self.tableView.reloadData()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (self.resultSearchController.active) {
return self.filteredKeys.count
} else {
return dict.count
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CountryTableViewCell
if(self.resultSearchController.active){
let key = self.filteredKeys[indexPath.row]
let dictionary = self.dict[key] as! NSDictionary
if
let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString),
let stateName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["State"] as? NSDictionary)!["name"] as? NSString),
let shortName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as? NSDictionary)!["short_name"] as? NSString)
{
cell.stateNameLabel.text = stateName as String
cell.cityNameLabel.text = cityName as String
cell.shortNameLabel.text = shortName as String
}
return cell
}else{
if let cityName = (((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["name"] as?NSString){
cell.cityNameLabel.text = cityName as String
}
return cell
}
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchWord = searchController.searchBar.text!
getCountriesNamesFromServer(searchWord)
self.filteredKeys.removeAll()
for (key, value) in self.dict {
let valueContainsSearchWord: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsSearchWord {
self.filteredKeys.append(key as! String)
}
}
self.tableView.reloadData()
}
func getCountriesNamesFromServer(searchWord:String){
let url:String = "http://localhost"
let params = ["keyword":searchWord]
ServerRequest.postToServer(url, params: params) { result, error in
if let result = result {
print("result is\(result)")
self.dict = result
}
}
}
}
字典结构:
{
0 = {
City = {
code = 10409;
"country_id" = 244;
id = 8727;
iso = "ZA-05";
lat = "-32.7833000";
longi = "26.8333000";
name = Alice;
"state_id" = 4379;
};
Country = {
id = 244;
name = "South Africa";
"short_name" = ZA;
};
State = {
"country_id" = 244;
id = 4379;
name = "Eastern Cape";
};
};
您只是在字典中搜索 ["City"]
键,应该也添加 ["Country"]
键。
for (key, value) in self.dict {
let valueContainsCity: Bool = (((value as? NSDictionary)?["City"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
let valueContainsCountry: Bool = (((value as? NSDictionary)?["Country"] as? NSDictionary)?["name"] as? String)?.uppercaseString.containsString(searchWord.uppercaseString) ?? false
if valueContainsCity || valueContainsCountry{ self.filteredKeys.append(key as! String) }
}
uppercaseString - 您的城市和国家名称是大写字符串吗?如果不做 lowerCaseString
这将为您获取城市 ID 和国家 ID。
(((self.dict["\(indexPath.row)"] as?NSDictionary)!["City"] as?NSDictionary)!["id"] as?NSString)
(((self.dict["\(indexPath.row)"] as?NSDictionary)!["Country"] as?NSDictionary)!["id"] as?NSString)