UIControl - changing assigned selectors: addTarget & removeTarget
States that you should remove the target before changing to another. However what if I am setting the target in cellForRowAtIndexPath? Should I remove the target before adding it again even if it is not changing? Will it call the method twice if I don't remove it or will it just overwrite it?
[cell.cellSwitch removeTarget:self action:@selector(notifySwitchChanged:) forControlEvents:UIControlEventValueChanged];
[cell.cellSwitch addTarget:self action:@selector(notifySwitchChanged:) forControlEvents:UIControlEventValueChanged];
Instead of adding/removing targets, I've found that if I'm already subclassing the UITableViewCell
, I'll add a new delegate and set the delegate to the view controller. That way, any methods called on the delegate can pass in the entire cell and therefore I can get the index path of the cell by calling UITableView
's indexPathForCell
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With