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

Switch In Custom Cell Fires On Table Reload #27

Open
bchristopher opened this issue Sep 23, 2012 · 1 comment
Open

Switch In Custom Cell Fires On Table Reload #27

bchristopher opened this issue Sep 23, 2012 · 1 comment

Comments

@bchristopher
Copy link

Hi,

I have a UITableView containing four rows. Each row has a custom cell with a DCRoundSwitch. The value-changed event is linked in IB to a switchChanged method. Each time a new cell with a switch is needed in cellForRowAtIndexPath I assign the new cell's switch to an ivar so I can control it programmatically like this:

       self.categorySwitch = ((FilterViewCell *)cell).sectionSwitch; 

The problem:
After I change a switch the first time it works perfectly, triggering the switchChanged method. Thereafter, every time a new cell is needed or the table is reloaded, it fires off the switchedChanged method without touching the switch. I've tried the removing all targets at the end of the switchChanged method like so but it still didn't work:

       [theSwitch removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];

This works perfectly using UISwitch.

Any suggestions? DCRoundSwitch appears to be an excellent alternative and I would like to use it.

Thanks

@ablackwoodim3
Copy link

DCRoundSwitch fires events for UIControlEventValueChanged even when the value is changed programmatically, rather than only when the user toggles the switch, as UISwitch does. I imagine that you're initializing the new cell's switch to the correct value, which fires the switchChanged method.

To make it behave like UISwitch, you can change the setOn:animated: method so that control events are not sent:

- (void)setOn:(BOOL)newOn animated:(BOOL)animated
{
    [self setOn:newOn animated:animated ignoreControlEvents:YES];
}

Remember to change the calls to setOn:animated: within tapped: and toggleDragged: so that they call setOn:animated:ignoreControlEvents: and pass true to the ignoreControlEvents parameter.

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

2 participants