2017-02-14 72 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() 
}