Hi all,
I have a custom cell with a label and a switch on the right hand side to denote whether the switch is activated or not.
I would like the switch to also be toggled if the user taps the row, rather than requiring them to actually use the switch.
I got this to work, but unlike when the user taps the switch itself, when they tap the row it does not animate the change - the switch just turns on and off with no animation at all. This doesn't look good so I need to fix it.
In my RowSelected override, I am doing this:
var cc = (CustomCell) tableView.CellAt(indexPath); cc.ToggleSwitch();
This is the relevant code in CustomCell.cs
private bool _enabled; public bool Enabled { get { return _enabled; } set { _enabled = value; this.CustomSwitch.On = _enabled; } } partial void CustomSwitch_ValueChanged(UISwitch sender) { //ignore this line, it just updates my data storage customController.Toggle(item); Enabled = sender.On; } public void ToggleSwitch() { CustomSwitch.On = !CustomSwitch.On; //tried this already and it did not work //UIView.Animate(0.3, () => { CustomSwitch.SendActionForControlEvents(UIControlEvent.ValueChanged); }); CustomSwitch.SendActionForControlEvents(UIControlEvent.ValueChanged); }
I hope someone knows how to resolve this
Answers
Change
CustomSwitch.On = !CustomSwitch.On;
toCustomSwitch.SetState(!CustomSwitch.On, true);
.SetState changes the state of the switch, and will animate the change if specified.
Thanks, that worked!