iOS无法在UISegmentedControl中选择段

问题描述 投票:0回答:1

我在UITableView的headerview中有一个UISegmentedControl。由于某种原因,我不能选择任何段,每当我这样做时,它会自动选择数组中的最后一段,我不能选择任何其他段。当我删除动作时,我可以选择哪个动作。不知道发生了什么。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    if (section == 1) {

        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 40)]; // x,y,width,height

        NSArray *itemArray = [NSArray arrayWithObjects:@"E01", @"E02", @"E03", @"E05", @"E06", @"T05", @"E17", @"E19", nil];
        control = [[UISegmentedControl alloc] initWithItems:itemArray];
        [control setFrame:CGRectMake(30.0, 0, (tableView.bounds.size.width) - 60, 40)];
        [control setSelected:YES];
        [control setSegmentedControlStyle:UISegmentedControlStylePlain];
        [control addTarget:self action:@selector(unitSegmentControl:) forControlEvents:UIControlEventValueChanged];

        [headerView addSubview:control];

        return headerView;
    }

    return nil;
}
ios arrays header tableview uisegmentedcontrol
1个回答
0
投票

我不知道调用setSelected在UISegmentedControl上做了什么,但我建议你使用:

control.selectedSegmentIndex = anNSInteger;

设置初始选定的段。我怀疑这是你的问题。

如果您有滚动表视图,还需要考虑其他事项,

viewForHeaderInSection

每当标题滚动到屏幕上时,将调用该部分,这意味着每当标题滚动到屏幕上时,您的分段控件将重新初始化,并且您将丢失所选索引。

除此之外,您可以发布unitSegmentControl选择器的代码。可能是它导致表视图重新加载并再次调用viewForHeaderInSection,从而重新初始化您的分段控制器。

© www.soinside.com 2019 - 2024. All rights reserved.