我有一个 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];
而且我还想覆盖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" )
{
// other actions
}
}
在这两种情况下,我要么直接单击 UISwitch,要么单击单元格,它会更改其状态并actSwitchChanged:
正确调用。
但是如果我单击单元格,我的 UISwitch 不会动画从一种状态到另一种状态的翻转,它只是在一个瞬间改变它的状态。
那么[actSwitch setOn:!value animated:YES]
它不足以告诉执行动画吗?
这是我设置和调用配置单元的方式:
- (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;
}
这就是我配置单元格的方式:
- (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];
[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;
}
}
但是当我一步一步调试时,经过这一步:
[actSwitch setOn:!value animated:YES]; // <-- HERE IS THE PROBLEM
actSwitch 仅在[self actSwitchChanged: actSwitch];
更改数据模型并调用后才更改其状态[self.tableView reloadData];
可能是因为我有两个带有 UISwitches 的单元格,并且它们之间存在一些冲突?可能有更好的方法从单元格中获取 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
}
}