【问题标题】:UISegmentedControl not respondingUISegmentedControl 没有响应
【发布时间】:2013-04-21 21:52:06
【问题描述】:

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

按要求截图:

【问题讨论】:

  • 可以发个截图吗?
  • 这个来自类似问题的答案为我解决了这个问题:stackoverflow.com/a/12436632/107821
  • 尝试将 clipsToBounds 设置为 YES,并查看分段控件是否仍然出现(以确保它实际上在您的视图范围内)。

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

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

【讨论】:

  • 我问了两个问题。我找到了第一个的答案(我添加了评论),这确实回答了第二个。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 1970-01-01
    • 2020-01-14
    • 2011-06-02
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    相关资源
    最近更新 更多