我在代码中向 UITableViewCell 添加了一个 UISwitch,并添加了一个目标/操作。问题是当 UISwitch 被触摸时,它开始动画到相反的值,buy 然后停止动画并返回到之前的值。所以基本上滑块会从OFF滑到ON的75%,然后又回到OFF,所以我不能切换它。切换后,我在 UITableView 中添加了一个部分,但我不知道为什么这不起作用。有人可以告诉我我的代码有什么问题吗?
这是我添加 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];
}