Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a UISegmentedControl in UITableView's header

I am using the following code to add a UISegmentedControl in UITableView. Everything works fine except that the UISegmentedControl is not responding to user interaction at all. What could be the matter?

- (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;

    }
}
like image 437
Anton Avatar asked Sep 16 '26 01:09

Anton


1 Answers

You should also have a corresponding heightForHeaderInSection method that looks like this:

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

If not, the control may still appear, but won't be drawn within any touchable area.

like image 196
dmur Avatar answered Sep 17 '26 14:09

dmur