如何使用RxSwift和RxRealm将领域对象绑定到UISwitch

时间:2018-02-02 07:34:55

标签: ios swift realm

我是研究RxSwift和RxRealm的新手。

首先让我告诉你我的ViewController。 enter image description here

此应用程序非常简单,添加用户名,年龄等,以及bool值 领域对象并在表格视图中显示

这是我的问题。

我想更新bool值以在tableview单元格中切换UISwitch。

但我找不到任何办法。

这是我的代码

    $uname = file_get_contents("php://input");
if(isset($uname)){
   echo $uname;
} 

这是CustomCell

import UIKit
import RealmSwift
import RxCocoa
import RxSwift
import RxRealm

class PersonInfo: Object {
    @objc dynamic var name = ""
    @objc dynamic var age = 0
    @objc dynamic var etc = ""
    @objc dynamic var isOn = false
}

class ViewController: UIViewController {
    @IBOutlet weak var bgButton: UIButton!
    @IBOutlet weak var nameTextfield: UITextField!
    @IBOutlet weak var ageTextfield: UITextField!{ didSet{ ageTextfield.keyboardType = .numberPad } }
    @IBOutlet weak var etcTextfield: UITextField!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var addItemButton: UIBarButtonItem!
    @IBOutlet weak var turnSwitch: UISwitch!{
        didSet {
            turnSwitch.isOn = false
        }
    }

    let bag = DisposeBag()
    var persons: Results<PersonInfo>!

    override func viewDidLoad() {
        super.viewDidLoad()

        let realm = try! Realm()
        persons = realm.objects(PersonInfo.self)


        let nameTextObservable = nameTextfield.rx.text.orEmpty.asObservable()
            .map{ text -> Bool in
                text.count >= 1
        }

        let ageTextObservable = ageTextfield.rx.text.orEmpty.asObservable()
            .map{ text -> Bool in
                text.count >= 1
        }

        // enable or disable Add button
        Observable.combineLatest(nameTextObservable, ageTextObservable){ $0 && $1 }
        .bind(to: addItemButton.rx.isEnabled).disposed(by: bag)

        // show datas in tableview
        Observable.collection(from: persons)
        .bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: CustomCell.self)){
            row, element, cell in

            cell.nameLabel.text = element.name
            cell.ageLabel.text = String(element.age)
            cell.switch.isOn = element.isOn

        }
        .disposed(by: bag)

        // add data in realm
        addItemButton.rx.tap
        .map{ _ -> PersonInfo in
            let newPerson = PersonInfo()
            newPerson.name = self.nameTextfield.text!
            newPerson.age = Int(self.ageTextfield.text!)!
            newPerson.etc = self.etcTextfield.text ?? "empty"
            newPerson.isOn = self.turnSwitch.isOn

            return newPerson
            }
            .bind(to: Realm.rx.add())
            .disposed(by: bag)


        // dismiss keyboard
        bgButton.rx.tap
            .subscribe(onNext:{
                self.nameTextfield.resignFirstResponder()
                self.ageTextfield.resignFirstResponder()
                self.etcTextfield.resignFirstResponder()
            })
            .disposed(by: bag)
    }
}

你能给我任何解决这个问题的线索吗?

提前谢谢。

0 个答案:

没有答案