RealmSwift数据过滤

时间:2016-02-13 18:51:07

标签: ios swift realm

假设我有很多城市对象。所以,城市ID将是我的主要密钥,因为它们不能重复。会有相同的城市名称,但是,不同的ID和不同的ShopAddress.Here是我的结果来自我的后端的json

{
     "CityName" : "NewYork",
     "ID" : 65,
     "CityCode" : "NY",
     "ShopAddress" : "NO.7\/8 17th quarter, 27th Street, LiverPool Township, NewYork."

},
{
    "CityName" : "NewYork",
    "ID" : 89,
    "CityCode" : "NY",
    "ShopAddress" : "NO.7\/8 17th quarter, 40th Street, West Stadium, NewYork."
},
{
    "CityName" : "Maimi",
    "ID" : 150,
    "CityCode" : "MI",
    "ShopAddress" : "NO.7\/8 17th quarter, 40th Street, West Stadium, Maimi."
}...etc

这是我的领域对象

import Foundation
import RealmSwift

class StoreList: Object {

dynamic var storeID : String = ""
dynamic var cityCode : String = ""
dynamic var cityName : String = ""
dynamic var shopAddress :String = ""

override static func primaryKey() -> String? {
    return "storeID"
}

}

如何获取与每个CityNames有关的CityNames数据和相应的商店地址?

for (var i = 0; i <= cityname.count; i++) {
        cityItems.append("City Names from realm")
        actualPositions.append(-1)

        var items = [String]()
        for (var i = 0; i < cityName["Newyork"].shopaddress.count; i++) {
            items.append("the locations shop address of each item from each cities")
        }

        self.locationItems.append(items)
    }

我想这样做,因为我希望在我的手风琴表视图中显示这一点。enter image description here

我写的上面的代码只是一个例子。会有很多错误。但是,我真的需要帮助过滤。任何帮助?

1 个答案:

答案 0 :(得分:0)

好的,这是我的解决方案:

首先,您遍历对象并获取所有城市并将其全部放入数组中。您还可以查找重复的城市,以便每个城市都有一个部分:

var cities = [string]()
var items = [int]()

// loop through your realm objects
for (var i = 0; i <= RealmObjects.count; i++) {

   // add every city to your array if it isn't already in your array
   if !cities.contains(RealmObjects.CityName) {
      cities.append(RealmObjects.CityName)
      items.append(1)
   }
   else {
      // get index of city
      let indexOfCity = cities.indexOf(RealmObjects.CityName)
      // get number of shops for the city
      var numberOfShops = cities[indexOfCity]
      numberOfShops += 1
      // raise items for section by 1
      items[indexOfCity] = numberOfShops
   }

}

// in your tableViewController add these sections as the title of your header
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

// this is your array including the cities
return self.cities

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// return the number of sections
return self.section.count

}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the number of rows for one section
return self.items[section] // not sure with this you have to get the current section 

}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)

    // Add the shops in your section
    cell.textLabel?.text = self.RealmObjects.ShopAddress[indexPath.row]

    return cell

}

我没试过这段代码......这是基本方法。 快速解释:

  1. 你必须将城市放入一个阵列(没有城市重复)
  2. 计算一个城市的商店数量
  3. 设置适合数组内城市的部分
  4. 为此设置一个部分的行数,您应该读出计算一个城市的商店的数组
  5. 设置节数
  6. 至少调用cellForRowAtIndexPath以设置单元格的文本