我有一个UITableView,其中包含一些包含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];
Run Code Online (Sandbox Code Playgroud)
我还想覆盖didSelectRowAtIndexPath:
方法来执行相应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" )
{
// …
Run Code Online (Sandbox Code Playgroud)