2015-06-30 75 views
3

我在中有國家代碼和國家名稱字符串數組。我只是過濾了國家名稱並在過濾後的tableView中顯示。但是,顯示時,我無法從原始tableview中獲取相應的國家/地區代碼。國家代碼未被過濾。請幫助我。如何過濾tableView單元格中的多個字段。如何在swift中過濾UITableView中的多個字段

我的代碼:

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 
    } 

我的輸出

enter image description here

回答

3

嘗試使用元組。

這是一個很棒的功能。

越來越countriescountry_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

+0

感謝@Raja。讓tmp:NSString = data.country。 我不明白這一行嗎? –

+0

我在詢問「data.country」 –

+0

「filtered = countryAndCode.filter」,在這一行中,我收到了錯誤消息。 **(country:String,code:String)與String **不相同** –

0

我以前有過類似的問題。我猜第一步是一樣的。創建一個國家和代碼在一個對象中的元組。

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 
     }) 

而在你的情況,你可以做& &,如果你想在這兩個領域進行篩選。

+0

如何使用let tmp:NSString = text而不指定屬性?如果我不使用text.attributeName,我得到一個錯誤 –

+0

你是對的。你得到什麼錯誤? – Desistreus

+0

我點擊無法將類型'(過程:字符串,請求者:字符串,創建:字符串,狀態:字符串)'的值轉換爲指定的類型'NSString' –

0

上面的回答中雨燕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.range(of: searchText, options: .caseInsensitive) 
    return range.location != NSNotFound 
})