gmarm / BetterSegmentedControl

An easy to use, customizable replacement for UISegmentedControl & UISwitch.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Updating Segmented Control from datasource without "re-calling" / sending valueChanged event.

amberkatyal opened this issue · comments

I have a use case of using this segmented control in section header. But on every reuse assignment from data source to section header using
header.yesNoSwitch.setIndex(question.answeredYes ? 0 : 1, animated: false)

It sends an event callback to
yesNoSwitch.addTarget(self, action: #selector(didChangeSwitchValue(_:)), for: .valueChanged)
to the selector which is giving incorrect row count.
What is the way to just update it without calling the event.
In other controls we can use like checkbox or radio button as they are used in tableviews. using "value/index" property and for callback a method "setValue/setIndex".

Thanks.

Hmm, I see your issue.

This is not possible at the moment, but it could be implemented by passing an additional parameter to the setIndex() method. I think it's a reasonable addition.

What I would suggest for you now is to handle this in your didChangeSwitchValue() by adding logic that would help you conclude whether the event is called by the section being reused or the user changing the value.

Yes it worked. Thanks.
`let oldValue = question.answeredYes ? 0 : 1

if oldValue == changedIndex { return }`

Support for customizing behavior added as part of #142 🙂