UISegmentedControl没有响应

时间:2013-04-28 00:04:39

标签: ios objective-c uisegmentedcontrol

我正在以编程方式将UISegmentedControl添加到分组的UITableView的底部。它显示正常,但当我点击任何选项时,它们不会突出显示。我在这做错了什么?另外,如何设置要突出显示的默认项目?

- (void)viewDidLoad
{
    [super viewDidLoad];

    // set up cacheControl
    NSArray* cacheDays = [NSArray arrayWithObjects: @"1", @"2", @"3", @"4", @"5", nil];
    self.cacheControl = [[UISegmentedControl alloc] initWithItems:cacheDays];
    [self.cacheControl setTintColor:[UIColor blackColor]];
    [self.cacheControl addTarget:self action:@selector(cacheSelection:) forControlEvents:UIControlEventValueChanged];
    self.cacheControl.frame = CGRectMake(10, 16, self.tbl.frame.size.width-20, 47);
    [self.cacheControl setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];

}

- (UIView *)tableView:(UITableView *)tv viewForFooterInSection:(NSInteger)section
{
    switch (section) {
        case 0:{
            UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.frame.size.width, tbl.contentInset.top)];
            footerView.backgroundColor = [UIColor clearColor];
            return footerView;
        }
        case 1:{
            UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tbl.frame.size.width, 16)];
            footerView.backgroundColor = [UIColor clearColor];

            [footerView setClipsToBounds:NO];

            [footerView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
            [footerView addSubview:self.cacheControl];

            return footerView;
        }
    }
    return nil;
}

截图,根据要求:

Settings-screen

2 个答案:

答案 0 :(得分:0)

要设置默认项目,请执行:[cacheControl setSelectedSegmentIndex:0],其中包含您要选择的索引。

答案 1 :(得分:0)

您应该从-tableView:heightForFooterInSection:协议实施UITableViewDelegate

这会让表格视图调整部分页脚的高度以适合您的视图,并允许它接收用户交互。