2016-02-13 55 views
0

假设我有很多城市对象。所以,城市ID将作为我的主键,因为它们不能重复。将会有相同的城市名称,但不同的ID和不同的ShopAddress.Here是我的从JSON结果这是从我的后端RealmSwift数据过滤

{ 
    "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

上面的代码我写的只是一个例子。会有很多错误。但是,我真的需要帮助filtering.Any帮助吗?

+0

我没有得到你想要达到的目标吗? – Anokrize

+0

你看过我的屏幕截图了吗?这就是我想要做的。但是,我需要首先获取数据保存到领域数据库。将会有许多城市。每个城市将包括商店address.Cities可能是重复的,但城市id不会重复。 –

+0

啊,所以你想显示你的城市下的商店(如果有一些可用于城市)? – Anokrize

回答

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. 至少调用cellForRowA tIndexPath为了设置单元格的文本
+0

好的,谢谢让我试试看,并会让你知道。 ; D –

+0

我可以在聊天时认识你吗? –