我在代码中添加了UISwitch到UITableViewCell,并添加了一个目标/动作。问题是,当触摸UISwitch时,它开始动画到相反的值,购买然后停止动画并返回到先前的值。所以基本上滑块会从关闭到关闭的75%,然后回到关闭状态,所以我无法切换。一旦它被切换,我添加一个部分到UITableView,我不知道为什么这不起作用。有人能告诉我,我的代码有什么问题吗?UISwitch不会改变值
这里就是我加入UISwitch:
self.forSaleSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(223.0f, 8.5f, 0.0f, 0.0f)];
[self.forSaleSwitch addTarget:self
action:@selector(switchTapped:)
forControlEvents:UIControlEventValueChanged];
self.forSaleSwitch.backgroundColor = [UIColor clearColor];
self.currentAnnotation.isHouseForSale = (self.forSaleSwitch.on) ? YES : NO;
[cell addSubview:self.forSaleSwitch];
这里是为UISwitch动作:
- (void)switchTapped:(id)sender
{
UISwitch *switcher = (UISwitch *)sender;
if (switcher.on)
{
[switcher setOn:NO animated:YES];
self.currentAnnotation.isHouseForSale = NO;
NSLog(@"OFF");
}
else
{
[switcher setOn:YES animated:YES];
self.currentAnnotation.isHouseForSale = YES;
NSLog(@"ON");
}
[self.tableView reloadData];
}
我完全删除了reloadData方法,并且问题没有解决。 –