【问题标题】:Adding a UISegmentedControl in UITableView's header在 UITableView 的 header 中添加 UISegmentedControl
【发布时间】:2012-07-06 02:57:57
【问题描述】:

我正在使用以下代码在 UITableView 中添加 UISegmentedControl。一切正常,除了 UISegmentedControl 根本不响应用户交互。可能是什么问题?

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 2) {            
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)]; // x,y,width,height    

        NSArray *itemArray = [NSArray arrayWithObjects: @"One", @"Two", nil];
        UISegmentedControl *control = [[UISegmentedControl alloc] initWithItems:itemArray];
        [control setFrame:CGRectMake(60.0, 0, 200.0, 40.0)];
        [control setSegmentedControlStyle:UISegmentedControlStylePlain];
        [control setSelectedSegmentIndex:0];
        [control setEnabled:YES];

        [headerView addSubview:control];
        return headerView;

    }
}

【问题讨论】:

    标签: objective-c ios uitableview uisegmentedcontrol


    【解决方案1】:

    您还应该有一个相应的 heightForHeaderInSection 方法,如下所示:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == 2) return 44.f;
        return 0.f;
    }
    

    如果没有,控件可能仍会出现,但不会在任何可触摸区域内绘制。

    【讨论】:

      猜你喜欢
      • 2014-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多