如何在swift中过滤UITableView中的多个字段
我在字符串数组中有国家代码和国家名称。我刚刚过滤了国家名称并显示在过滤后的表格视图中。但是,在显示时,我无法从原始tableview中获取相应的国家/地区代码。国家代码未被过滤。请帮帮我。如何筛选tableView单元格中的多个字段 我的代码:如何在swift中过滤UITableView中的多个字段,swift,uitableview,uisearchbar,Swift,Uitableview,Uisearchbar,我在字符串数组中有国家代码和国家名称。我刚刚过滤了国家名称并显示在过滤后的表格视图中。但是,在显示时,我无法从原始tableview中获取相应的国家/地区代码。国家代码未被过滤。请帮帮我。如何筛选tableView单元格中的多个字段 我的代码: var countries = [String]() for countryCodes : AnyObject in NSLocale.ISOCountryCodes() { let dictionary : NSDictionary = NSD
var countries = [String]()
for countryCodes : AnyObject in NSLocale.ISOCountryCodes() {
let dictionary : NSDictionary = NSDictionary(object:countryCodes, forKey:NSLocaleCountryCode)
let identifier : NSString? = NSLocale.localeIdentifierFromComponents(dictionary)
if ((identifier) != nil) {
let country : NSString = NSLocale.currentLocale().displayNameForKey(NSLocaleIdentifier, value: identifier!)!
countries.append(country)
println(countries)
}
}
println(countries)
var country_codes = [String]()
country_codes = NSLocale.ISOCountryCodes() as [String]
println(country_codes) //Working Successfully.
//SEARCH BAR WHILE TYPING TEXT
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
if(countElements(searchBar.text) >= 3)
{
searchActive = true
filtered = countries.filter({ (text) -> Bool in
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
//I filtered only one fields. So, Filtered results are working fine, but, I am having one more fields in tableview cell. That is displaying different datas. I dont know how to filtered multiple fields.
if(filtered.count == 0 || countElements(searchBar.text) == 0)
{
searchActive = false
self.countryTableView.reloadData()
}
else
{
searchActive = true
self.countryTableView.reloadData()
}
}
else
{
searchActive = false
self.countryTableView.reloadData()
}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = countryTableView.dequeueReusableCellWithIdentifier("country", forIndexPath: indexPath) as country_tblCell
//println("Cell for ROws \(searchActive)")
if(searchActive)
{
if(filtered.count == 0 || countElements(searchBar.text) == 0)
{
searchActive = false
self.countryTableView.reloadData()
}
else
{
println("First Index \(indexPath.row)")
cell.country_Label.text = filtered[indexPath.row]
cell.countryCodeLabel.text = countryCodes[indexPath.row] //Here need to display filtered datas for Country code.
println("Second Index \(indexPath.row)")
}
}
else
{
println("Normal First Index \(indexPath.row)")
cell.selectionStyle = .None
cell.country_Label.text = countries[indexPath.row]
cell.countryCodeLabel.text = countryCodes[indexPath.row]
cell.layer.borderColor = validateBorderColor.CGColor
cell.layer.borderWidth = 1
println("Normal Second Index \(indexPath.row)")
}
return cell
}
我的输出
尝试使用元组
这是swift中的一个重要功能
获取国家/地区
和国家/地区代码
将它们添加到此元组
var countryAndCode: [(country:String , code: String)] = []
for i in 0...coutries.count {
countryAndCode.append(country:countries[i] , code:country_codes[i])//this shows error in XCode some times
//if the above code doesn't compile then use the below code
countryAndCode += [(country:countries[i] , code:country_codes[i])]
}
cell.country_Label.text = filtered[indexPath.row].country
cell.countryCodeLabel.text = filtered[indexPath.row].code
然后在此元组上执行筛选:
filtered = countryAndCode.filter({ (data) -> Bool in
let tmp: NSString = data.country // the name from the variable decleration
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
filtered = countryAndCode.filter({ (data) -> Bool in
let tmp: NSString = data.country // the name from the variable decleration
let range = tmp.range(of: searchText, options: .caseInsensitive)
return range.location != NSNotFound
})
最后,在cellforrowatinexpath
方法中使用此元组
var countryAndCode: [(country:String , code: String)] = []
for i in 0...coutries.count {
countryAndCode.append(country:countries[i] , code:country_codes[i])//this shows error in XCode some times
//if the above code doesn't compile then use the below code
countryAndCode += [(country:countries[i] , code:country_codes[i])]
}
cell.country_Label.text = filtered[indexPath.row].country
cell.countryCodeLabel.text = filtered[indexPath.row].code
有关元组的更多信息,请访问我之前遇到过类似的问题。我想第一步也是一样的。创建一个元组,其中国家和代码位于一个对象中
var countryAndCode: [(country:String , code: String)] = []
然后我基本上对字段使用OR运算符。您的代码:
filtered = countries.filter({ (text) -> Bool in
let tmp: NSString = text.attribute
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
变成这样:
filtered = countries.filter({ (text) -> Bool in
let tmp: NSString = text.attribute
let range = tmp.country.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) || tmp.code.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
在您的情况下,如果您想在这两个字段上进行筛选,您可以执行&&操作。以上以Swift 3.0回答
var countryAndCode: [(country:String , code: String)] = []
for i in 0...coutries.count-1 {
countryAndCode += [(country:countries[i] , code:country_codes[i])]
}
然后在此元组上执行筛选:
filtered = countryAndCode.filter({ (data) -> Bool in
let tmp: NSString = data.country // the name from the variable decleration
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
filtered = countryAndCode.filter({ (data) -> Bool in
let tmp: NSString = data.country // the name from the variable decleration
let range = tmp.range(of: searchText, options: .caseInsensitive)
return range.location != NSNotFound
})
谢谢@Raja。让tmp:NSString=data.country。我听不懂这一行?我问的是“data.country”“filtered=countryAndCode.filter”,这一行我有一个错误。(国家/地区:字符串,代码:字符串)与不相同String@McDonal_11对象名称
国家/地区
。因此,将来要引用元组中的country部分,我们将其命名为:countryAndCode[1]。country
。在这里,当使用.filter
方法进行过滤时,我们获取数组的每个对象,它是一个元组,并将其分配给数据
。因此,要访问元组的country
部分,我们使用data.country
yes@Raja!!我明白了。“filtered=countryAndCode.filter”,,在这一行我得到了一个错误。(国家/地区:字符串,代码:字符串)与字符串不相同。我不知道是什么原因导致了这个错误?在不指定属性的情况下,如何使用let tmp:NSString=text?如果我不使用text.attributeName,我会出错。我无法将类型为“(进程:String,请求者:String,创建:String,状态:String)”的值转换为指定的类型“NSString”,因此countries数组中的对象是字符串,但clojure需要NSString,并且显然无法转换。已经有一段时间了。看看这个问题和答案: