7

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

2 回答 2

7

只需延迟调用您的 actSwitchChanged 函数:

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];
于 2011-10-30T08:16:16.473 回答
0

斯威夫特 4

使用委托协议创建一个 UITableViewCell 子类。我添加了一个标题标签和一个开关,如下所示:

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)
    }    

}

在您的 UITableViewController 中添加委托方法,如下所示:

class UITableViewController: CellWithSwitchDelegate {

    func didToggleSwitch(on: Bool) {
        // This is the delegate function that's called when the switch is toggled
        print("toggled switch")
    }
}

在你的 UITableViewController 中添加如下到你的 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)
}
于 2017-12-12T22:52:08.553 回答