2017-02-14 73 views
0

我想通過一些過濾器來過濾的UITableView:國家內容的UITableView

  • 地區價格
  • 範圍

這裏是我的TVC前怎麼看過濾

TVC ScreenShot

用戶CA N乘區域 「美洲」 應用過濾器並只獲得

Filtered TVC

按地區是我的過濾代碼

func useFilter() { 
    countries.forEach({ (country) in 

     let countryRegion = countryToRegionDic[country] 

     if (countryRegion != nil && filters.contains(countryRegion!)){ 

      self.newCountries.append(country) 
      self.countries = newCountries.removeDuplicates() 
      self.tableView.reloadData() 
     } 
    }) 
} 

詞典搜索地區 -

let countryToRegionDic = [  
"United States" : "America", 
"Canada" : "America"] 

問題在這種情況下,我得到排序的國家,但你可以看到「古巴」得到新的價格 - 過濾之前是275,之後 - 100(如tableView中的最後一個元素)

第二種情況下,按價格範圍過濾器,這是我如何做到這一點

func filterByPrice() { 
    prices = prices.filter({$0 >= (100) && $0 <= 200}) 
    tableView.reloadData() 
} 

而如果當我使用過濾器通過價格我的應用得到了在方法 的tableView(_的tableView崩潰的原因: UITableView的,numberOfRowsInSection段中:int) - >內部

我回countriesArray.count

+0

你填充上的tableView兩個不同的陣列上的數據? – Aragunz

+0

是的。我是否需要爲兩個值(地點和價格)使用結構? –

+0

Ofc bro。 cuz什麼烏爾都在過濾一個數組,而另一個則保持原樣。 – Aragunz

回答

0
Some magic here: 

var bound = [ShowData]() 
var newBound = [ShowData]() 

func useFilter() { 
    bound.forEach({ 
     let countryRegion = countryToRegionDic[$0.place] 
     if let countryRegion = countryRegion, filters.contains(countryRegion) { 

      let a = $0.place 
      let b = $0.price 

      let lol = ShowData(place: a, price: b) 
      newBound.append(lol) 
      bound = newBound 
     } 
    }) 
    tableView.reloadData() 
}