manishkkatoch / SimpleTwoWayBindingIOS

An ultra light, ultra simple two way binding library for IOS and Swift.
MIT License
165 stars 48 forks source link

textField.observe has issues with cell reuse #11

Open sigmundfridge opened 5 years ago

sigmundfridge commented 5 years ago

If I have multiple text fields in a table observing changes to a model property, then as the cells are reused they start observing multiple properties. This can be fixed by checking that the observable value returned matches the model property, but it would be better to clear observables when a cell is reused (and as far as I know you can only add obervables). For the example below, changing pickerTwo value will sometimes also update rowOne as it has previously been observing rowTwo (under cell reuse rules)

case .rowOne:
    cell.textField.observe(for: vModel.propertyOne) { (prop1) in
        //hack way of ensuring observable is right one (as cannot remove "observe" for cell reuse)
        if prop1 == vModel.propertyOne.value {
            cell.textField.text = getPropOneValue()
        }
    }
    cell.textField.text = vModel.getPropOneValue()
    cell.textField.inputView = pickerOne
case .rowTwo:
    cell.textField.observe(for: vModel.propertyTwo) { (prop2) in
        if prop2 == vModel.propertyTwo.value {
            cell.textField.text = getPropTwoValue()
        }
    }
    cell.textField.text = vModel.getPropTwoValue()
    cell.textField.inputView = pickerTwo
sigmundfridge commented 5 years ago

Actually that hack way doesn't help.....

sigmundfridge commented 5 years ago

4 Related, but for bind rather than observe

sigmundfridge commented 5 years ago

The only way I can see to fix is to either observe everything (rather than bind) and compare index paths, or to override "prepareForReuse" and entirely replace the text field. Neither is ideal. When I have more time I can look at a "clear all bindings" method that can be applied at start of "cell for index path"