在UITableView的标题中添加UISegmentedControl

Ant*_*ton 3 objective-c uitableview uisegmentedcontrol ios

我使用以下代码在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;

    }
}
Run Code Online (Sandbox Code Playgroud)

dmu*_*mur 6

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

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

如果不是,则控件可能仍然出现,但不会在任何可触摸区域内绘制.