0

我遇到了一个非常奇怪的错误。有问题的视图是 a UITableView,其中每个单元格都包含 aUISwitch作为其accessoryView。我已将其连接起来,以便触发值的变化,- (IBAction)toggleEnabled:(UISwitch *)theSwitch如下所示:

- (IBAction)toggleEnabled:(UISwitch *)theSwitch
{
    UITableViewCell *cell = (UITableViewCell *)theSwitch.superview.superview;
    NSIndexPath *indexPath = [(UITableView *)self.tableView indexPathForCell:cell];
    Alarm *alarm = [self.fetchedResultsController objectAtIndexPath:indexPath];
    alarm.unrelatedProperty = @"This property is unrelated to the switch.";
}

错误的行为是开关立即恢复到其原始状态。只要你一弹,它就会弹回来。我做了更多的测试,将我的代码更改为:

- (IBAction)toggleEnabled:(UISwitch *)theSwitch
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    Alarm *alarm = [self.fetchedResultsController objectAtIndexPath:indexPath];
    alarm.unrelatedProperty = @"This property is unrelated to the switch.";
}

现在,恢复行为只发生在列表中的第一个开关上(第 0 行)。所有其他开关都工作正常,这让我相信这与修改给定的对象有关indexPath。开关可以正常使用此代码,所以我很确定这是由于分配:

- (IBAction)toggleEnabled:(UISwitch *)theSwitch
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    Alarm *alarm = [self.fetchedResultsController objectAtIndexPath:indexPath];
}

有人知道会发生什么吗?谢谢!

4

0 回答 0