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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With