我在 字符串数组 中有国家代码和国家名称。我只是过滤了国家名称并显示在过滤后的 tableView 中。但是,在显示时,我无法从原始表格 View 中获取相应的国家/地区代码。国家代码未被过滤。请帮助我。如何过滤 tableView 单元格中的多个字段。
我的代码:
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 的一个很棒的特性。
得到countries
和country_codes
之后
将它们添加到这个元组中
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])]
}
然后对这个元组执行过滤:
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
})
最后在 cellForRowAtIndexPath
方法中使用这个元组
cell.country_Label.text = filtered[indexPath.row].country
cell.countryCodeLabel.text = filtered[indexPath.row].code
有关元组的更多信息,您可以访问 http://www.raywenderlich.com/75289/swift-tutorial-part-3-tuples-protocols-delegates-table-views
关于swift - 如何快速过滤 UITableView 中的多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31130717/