2

我正在以编程方式将 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;
}

截图,按要求:

设置画面

4

2 回答 2

0

您应该-tableView:heightForFooterInSection:UITableViewDelegate协议中实现。

这将使表格视图调整节页脚的高度以适合您的视图并允许它接收用户交互。

于 2013-04-28T15:15:36.523 回答
0

要设置默认项目,请执行以下操作:[cacheControl setSelectedSegmentIndex:0]使用您要选择的索引。

于 2013-04-28T14:17:38.310 回答