我想做的是当我的 parent 在 Realm 中时访问 child 。在此示例中,我有一个简单的 TableView ,我想在访问父级时用子级填充该 TableView 。我正在努力解决的部分是在访问 parent 时试图找到 child 。
这是我试图访问子级的 viewController:
import UIKit
import Realm
import RealmSwift
class OtherViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var otherTableView: UITableView!
var realm: Realm!
var realmedData = ""
var realmList: Results<Realmed> {
get {
return realm.objects(Realmed.self)
}
}
var realmTwoList: Results<RealmTwo> {
get {
return realm.objects(RealmTwo.self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
realm = try! Realm()
self.otherTableView.delegate = self
self.otherTableView.dataSource = self
// Do any additional setup after loading the view.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var counted = realm.objects(RealmTwo.self).filter("realmLbl == %@", realmedData)
return counted.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath) as! OtherTableViewCell
var celledItem = realm.objects(Realmed.self)
for item in celledItem {
for items in item.realmTwo {
cell.otherLbl.text = "\(items.spanish)"
}
}
return cell
}
}
这是我为位于以下行的单元格尝试的另一种方法:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath) as! OtherTableViewCell
var celledItem = realm.objects(Realmed.self)
for item in celledItem {
for items in item.realmTwo {
cell.otherLbl.text = "\(items.spanish)"
}
}
return cell
}
这是父 Realm 类:
import Foundation
import Realm
import RealmSwift
class Realmed: Object {
@objc dynamic var label = ""
@objc dynamic var romanNum = ""
@objc dynamic var txt = ""
let realmTwo = List<RealmTwo>()
override static func primaryKey() -> String {
return "label"
}
convenience init(label: String, romanNum: String, txt: String) {
self.init()
self.label = label
self.romanNum = romanNum
self.txt = txt
}
}
这是子级的 Realm 类:
import Foundation
import UIKit
import Realm
import RealmSwift
class RealmTwo: Object {
@objc dynamic var realmLbl = String()
@objc dynamic var spanish = String()
@objc dynamic var french = String()
let realmed = LinkingObjects(fromType: Realmed.self, property: "realmTwo")
convenience init(realmLbl: String, spanish: String, french: String) {
self.init()
self.realmLbl = realmLbl
self.spanish = spanish
self.french = french
}
}
当我按原样运行时,填充 TableView 的唯一内容是保存到 Realm 的最后一个值。 在此示例中,子级是字符串:“Uno”和“Un”,我希望它们都填充 tableView,但 tableView 仅由 Realm 中的最后一个值填充,在本例中为“Un”。 通过研究我发现这是因为我循环遍历 Realm 值来获取 child 。问题在于,到达子级的唯一方法是使用循环,但它无法填充 tableView。这似乎是一个双输的局面。
我很好奇的是,当您在 Realm 中有父级时如何访问子级,以便我能够填充 tableView。
如果您需要什么,请询问。谢谢您
最佳答案
我想我得到了一些东西。我想我需要的是一个数组,而将List改为数组似乎是不明智的,所以我所做的就是从List中获取内容并将其放入数组中,因为这样更容易将数据放入数组中的表格 View 。
这是我创建的数组:
var realmArr = [String]()
这是 tableView 的行单元格:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "otherCell", for: indexPath) as! OtherTableViewCell
var celledItem = realm.objects(Realmed.self)
for item in celledItem {
for items in item.realmTwo {
self.realmArr.append(items.spanish)
}
}
cell.otherLbl.text = "\(realmArr[indexPath.row])"
return cell
}
我不确定这是否可以,但这是我唯一能想到的。 感谢您的所有帮助。
关于ios - 如何快速从父子关系中的父级获取子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679453/