gpt4 book ai didi

ios - RealmSwift 数据过滤

转载 作者:行者123 更新时间:2023-11-30 13:42:38 29 4
gpt4 key购买 nike

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

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

这是我的 Realm 对象

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

我想这样做,因为我想在我的 Accordion TableView 中显示这样的内容。 enter image description here

我写的上面的代码只是一个例子。会有很多错误。但是,我确实需要过滤方面的帮助。有什么帮助吗?

最佳答案

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

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

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 才能设置单元格的文本

关于ios - RealmSwift 数据过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383956/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com