在 2 个部分中分离 Realm Bool 数据 Tableview
Seperate Realm Bool data in 2 sections Tableview
美好的一天!我有一个小问题。我想通过 Bool 值(true 和 false)从 Realm DB 中分离数据。如果 Bool 数据 currencyStatusCode == true 则在第一个 tableview 部分 "Active accounts" 显示货币名称、currencyCode 和余额,如果 Bool 数据 currencyStatusCode == false 则在第二部分 "Inactive accounts"。以及我添加数据库外观的图像。非常感谢您的帮助。
var sections = ["Active accounts", "Inactive accounts"]
@IBOutlet weak var accountManagerTableView: UITableView!
let realm = try? Realm()
let accounts = try! Realm().objects(currencyAccounts.self).sorted(byKeyPath: "currencyID")
var accountManager: currencyAccounts?
var accountsRecord: Results<currencyAccounts> {
get {
return realm!.objects(currencyAccounts.self)
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return accounts.count
/* switch (section) {
case 0:
return accounts.count
break;
case 1:
return accounts.count
break;
default:
break;
}
return section
*/
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = accountManagerTableView.dequeueReusableCell(withIdentifier: "activeCurrencyCell", for: indexPath) as! accountManagerTableViewCell
let sortingtInTableView = realm?.objects(currencyAccounts.self).sorted(byKeyPath: "currencyID", ascending: true)
let currentUserBalances = sortingtInTableView![indexPath.row]
cell.currencyFullName.text = currentUserBalances.currencyName
cell.currencyTitle.text = currentUserBalances.currencyCode
cell.selectedAccountBalance.text = String(currentUserBalances.currencyBalance)
return cell
}
Realm DB data
您再次使用过滤器 Realm 对象本身。详情请参考 documentation。
请找到过滤活动和非活动帐户的代码:
let realm = try! Realm()
let activeAccounts = realm.objects(Dog.self).filter("currencyStatusCode = true").sorted(byKeyPath: "currencyID")
// Persist your data easily
try! realm.write {
realm.add(activeAccounts)
}
let realm = try! Realm()
let inactiveAccounts = realm.objects(Dog.self).filter("currencyStatusCode = false").sorted(byKeyPath: "currencyID")
// Persist your data easily
try! realm.write {
realm.add(inactiveAccounts)
}
美好的一天!我有一个小问题。我想通过 Bool 值(true 和 false)从 Realm DB 中分离数据。如果 Bool 数据 currencyStatusCode == true 则在第一个 tableview 部分 "Active accounts" 显示货币名称、currencyCode 和余额,如果 Bool 数据 currencyStatusCode == false 则在第二部分 "Inactive accounts"。以及我添加数据库外观的图像。非常感谢您的帮助。
var sections = ["Active accounts", "Inactive accounts"]
@IBOutlet weak var accountManagerTableView: UITableView!
let realm = try? Realm()
let accounts = try! Realm().objects(currencyAccounts.self).sorted(byKeyPath: "currencyID")
var accountManager: currencyAccounts?
var accountsRecord: Results<currencyAccounts> {
get {
return realm!.objects(currencyAccounts.self)
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return accounts.count
/* switch (section) {
case 0:
return accounts.count
break;
case 1:
return accounts.count
break;
default:
break;
}
return section
*/
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = accountManagerTableView.dequeueReusableCell(withIdentifier: "activeCurrencyCell", for: indexPath) as! accountManagerTableViewCell
let sortingtInTableView = realm?.objects(currencyAccounts.self).sorted(byKeyPath: "currencyID", ascending: true)
let currentUserBalances = sortingtInTableView![indexPath.row]
cell.currencyFullName.text = currentUserBalances.currencyName
cell.currencyTitle.text = currentUserBalances.currencyCode
cell.selectedAccountBalance.text = String(currentUserBalances.currencyBalance)
return cell
}
Realm DB data
您再次使用过滤器 Realm 对象本身。详情请参考 documentation。 请找到过滤活动和非活动帐户的代码:
let realm = try! Realm()
let activeAccounts = realm.objects(Dog.self).filter("currencyStatusCode = true").sorted(byKeyPath: "currencyID")
// Persist your data easily
try! realm.write {
realm.add(activeAccounts)
}
let realm = try! Realm()
let inactiveAccounts = realm.objects(Dog.self).filter("currencyStatusCode = false").sorted(byKeyPath: "currencyID")
// Persist your data easily
try! realm.write {
realm.add(inactiveAccounts)
}