小编zka*_*aje的帖子

以编程方式更改动画的UISwitch状态

我有一个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)

iphone animation objective-c uikit uiswitch

5
推荐指数
1
解决办法
9371
查看次数

标签 统计

animation ×1

iphone ×1

objective-c ×1

uikit ×1

uiswitch ×1