Change the state of UISwitch with animation programmatically
I have a UITableView with some cell's that containt UISwitches:
UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.lock_when_inactive boolValue];
[actSwitch setOn: value animated:NO];
And I also want to overwrite didSelectRowAtIndexPath:
method to perform toggling of respective UISwitch:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellType = [self typeOfCellForIndexPath:indexPath];
if ( cellType == @"CellWithSwitch" )
{
UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
BOOL value = [actSwitch isOn];
[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
[self actSwitchChanged: actSwitch];
}
else if ( cellType == @"CellWithoutSwitch" )
{
// other actions
}
}
In both situations, either I click on the UISwitch directly or by clicking on cell, it changes it's state and call's actSwitchChanged:
correctly.
But in case if I click on the cell, my UISwitch doesn't animate the flipping from one state to another, it just changes its state in a single moment.
So [actSwitch setOn:!value animated:YES]
isn't enough it to tell to perfom animation?
Here's how I set & call configure cell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = nil;
NSString *EnabledCellIdentifier = [self typeOfCellForIndexPath:indexPath];
cell = [tableView dequeueReusableCellWithIdentifier:EnabledCellIdentifier];
if (cell == nil)
{
cell = [self cellForType:EnabledCellIdentifier];
}
[self cofigureCell:cell forIndexPath:indexPath];
return cell;
}
This is how I configure cell:
- (void)cofigureCell:(UITableViewCell*)cell forIndexPath:(NSIndexPath*)indexPath {
switch ( indexPath.section )
{
case 0:
// not this
case 1:
{
switch ( indexPath.row )
{
case 0:
{
cell.textLabel.text = @"Limit passwords attempts";
UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
开发者_如何学C [actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.limit_password_attempts boolValue];
[actSwitch setOn: value animated:NO];
break;
}
//other rows of this section here are being configured
}
break;
}
case 2:
{
switch ( indexPath.row )
{
case 0:
{
cell.textLabel.text = @"Lock when inactive";
UISwitch* actSwitch = (UISwitch*)[cell viewWithTag: SWITCH_TAG];
[actSwitch addTarget: self
action: @selector(actSwitchChanged:)
forControlEvents: UIControlEventValueChanged];
BOOL value = [appSettings.lock_when_inactive boolValue];
[actSwitch setOn: value animated:NO];
break;
}
//other rows of this section here are being configured
}
break;
}
default:
break;
}
}
But when I debug step by step, and go through this step:
[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
The actSwitch changes its state only after [self actSwitchChanged: actSwitch];
changed data model and calls [self.tableView reloadData];
May be the reason is that I have two cells with UISwitches, and there accures some conflict between them? May be there is a better way to get UISwitch from the cell then this code of mine?:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellType = [self typeOfCellForIndexPath:indexPath];
if ( cellType == @"CellWithSwitch" )
{
UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
BOOL value = [actSwitch isOn];
[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
[self actSwitchChanged: actSwitch];
}
else if ( cellType == @"CellWithoutSwitch" )
{
// other actions
}
}
Just make call to your actSwitchChanged function with some delay:
UISwitch* actSwitch = (UISwitch*)[[[self tableView] cellForRowAtIndexPath:indexPath ] viewWithTag: SWITCH_TAG];
BOOL value = [actSwitch isOn];
[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
[self performSelector:@selector(actSwitchChanged:) withObject:actSwitch afterDelay:0.55];
SWIFT 4
Create a UITableViewCell Subclass, with delegate protocol. I added a title label and a switch, like so:
protocol CellWithSwitchDelegate: class {
func didToggleSwitch(on: Bool)
}
class CellWithSwitchTableViewCell: UITableViewCell {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var toggleSwitch: UISwitch!
weak var delegate: CellWithSwitchDelegate?
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
@IBAction func switchWasToggled(_ sender: UISwitch) {
delegate?.didToggleSwitch(on: sender.isOn)
}
}
In your UITableViewController add the delegate method like so:
class UITableViewController: CellWithSwitchDelegate {
func didToggleSwitch(on: Bool) {
// This is the delegate function that's called when the switch is toggled
print("toggled switch")
}
}
In your UITableViewController add as follows to your didSelectRowAtIndexPath:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CellWithSwitchTableViewCell
let settingsSwitch = cell.toggleSwitch!
didToggleSwitch(on: !settingsSwitch.isOn)
settingsSwitch.setOn(!settingsSwitch.isOn, animated: true)
}
精彩评论