Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

textField.observe has issues with cell reuse #11

Open
sigmundfridge opened this issue Feb 14, 2019 · 3 comments
Open

textField.observe has issues with cell reuse #11

sigmundfridge opened this issue Feb 14, 2019 · 3 comments

Comments

@sigmundfridge
Copy link

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
Copy link
Author

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

@sigmundfridge
Copy link
Author

#4 Related, but for bind rather than observe

@sigmundfridge
Copy link
Author

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant