在2个区域中分开Realm Bool数据Tableview

时间:2018-06-29 03:16:05

标签: ios swift uitableview realm sections

美好的一天!我有一点问题。我想按Bool值(真和假)将数据与Realm DB分开。如果Bool数据currencyStatusCode == true,则在第一个表视图部分“活动帐户”中显示currencyName,currencyCode和余额;如果Bool数据currencyStatusCode == false,则进入第二部分“无效帐户”。以及添加数据库外观的图像。非常感谢您的帮助。

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

1 个答案:

答案 0 :(得分:0)

您再次使用过滤器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)
}
相关问题