Ios UISegmentedControl不';t取消选择旧段

Ios UISegmentedControl不';t取消选择旧段,ios,objective-c,uitableview,uisegmentedcontrol,Ios,Objective C,Uitableview,Uisegmentedcontrol,我在UITableView表行中有一个UISegmentedControl。将激发选择,并高亮显示新线段。问题是,旧段仍处于选中状态 如果我关闭并重新打开包含该表的popover,将显示正确的段索引 我正在运行XCode 6.1,并在iOS 7.1模拟器上进行测试 感谢您的帮助 UITableViewCell *segmentCell = [tableView dequeueReusableCellWithIdentifier:segmentCellIdentifier]; if (segmen

我在UITableView表行中有一个UISegmentedControl。将激发选择,并高亮显示新线段。问题是,旧段仍处于选中状态

如果我关闭并重新打开包含该表的popover,将显示正确的段索引

我正在运行XCode 6.1,并在iOS 7.1模拟器上进行测试

感谢您的帮助

UITableViewCell *segmentCell = [tableView dequeueReusableCellWithIdentifier:segmentCellIdentifier];
if (segmentCell == nil) {
segmentCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:segmentCellIdentifier];
    }
segmentCell.backgroundColor = [UIColor clearColor];
segmentCell.backgroundView = nil;
NSArray *segmentItems = [NSArray arrayWithObjects: NSLocalizedString(@"settingsBackgroundCork", @"Cork - Select cork background theme"), NSLocalizedString(@"settingsBackgroundDark", @"Dark - Select dark background theme"), NSLocalizedString(@"settingsBackgroundLight", @"Light - Select light background theme"), nil];
self.backgroundSegmentedControl = [[UISegmentedControl alloc] initWithItems: segmentItems];
[self.backgroundSegmentedControl addTarget:self action:@selector(backgroundControlChanged:) forControlEvents: UIControlEventValueChanged];
self.backgroundSegmentedControl.frame = CGRectMake(10, 6, 300, 32);
NSInteger background = [[NSUserDefaults standardUserDefaults] integerForKey:kUserDefaultsBackgroundSelection];
self.backgroundSegmentedControl.selectedSegmentIndex = background;
self.backgroundSegmentedControl.momentary = NO;
[segmentCell.contentView addSubview:self.backgroundSegmentedControl];
cellToReturn = segmentCell;

以下是在选择段时调用的方法:

- (void)backgroundControlChanged:(UISegmentedControl *)control
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:control.selectedSegmentIndex forKey:kUserDefaultsBackgroundSelection];
self.backgroundSegmentedControl.selectedSegmentIndex = control.selectedSegmentIndex;
[self.backgroundSegmentedControl setNeedsDisplay];
[[NSNotificationCenter defaultCenter] postNotificationName:kPageBackgroundShouldChangeNotification object:nil];
}

奇怪的虫子-我以前从未见过这个。两项建议:

  • 尝试在处理程序中再次设置
    selectedSegmentIndex
    ,然后
  • 也可以尝试在控件上调用
    setNeedsDisplay
默认情况下,
瞬时
,因此您不需要设置它

此外,在
selectedSegmentIndex
文档中,它还指出:

默认值为UISegmentedControlNoSegment(未选择段),直到用户触摸段。将此属性设置为-1可关闭当前选择


也许您还想试试最后一件事。

我可以通过将对setSelectedSegmentIndex的调用移动到setNeedsLayout函数内部,而不是在创建UISegmentedControl之后立即调用来解决这个问题。当然,我还必须创建一个变量来跟踪应该选择哪个段。

也有同样的问题。通过在添加新实例之前从superview中删除segmentedControl的上一个实例来解决此问题

在将segmentedControl的新实例添加到父视图之前,添加下面提到的3行代码以删除以前的segmentedControl

self.backgroundSegmentedControl.tag = 101
if let foundView2 = segmentCell.contentView.viewWithTag(101) {
    foundView2.removeFromSuperview()
}
[segmentCell.contentView addSubview:self.backgroundSegmentedControl];

谢谢你的建议。我试过你的前两个建议,都没有用。更新了上面的代码示例。我还尝试使用-1关闭选择,但是分段控件根本不注册选择。您的表视图是否滚动,如果是,您是否仅在滚动后才看到此效果?表视图具有滚动功能。也就是说,该元素位于顶部,不需要显示任何滚动即可看到。当我测试这一点时,我没有滚动(即,行没有被重绘)。我复制您的问题的唯一方法是滚动。编写代码时遇到的一个问题是,在重用单元时,将在旧分段控制器的基础上添加新分段控制器。最好创建一个子类单元格,并在其init方法中添加分段控件。或者,在CellFroRowatineXpath中添加分段控件之前,可以检查单元格是否包含分段控件。我也遇到了同样的奇怪问题。如果我将分段控件的创建放在dispatch_async(dispatch_get_main_queue(),^{})块中,结果会略有不同,但它的行为仍然不正确。。我求助于完全从头开始做一个类似的控制。这的确是一个奇怪的问题。