我正在使用以下代码在 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;
}
}
最佳答案
您还应该有一个相应的 heightForHeaderInSection 方法,如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 2) return 44.f;
return 0.f;
}
否则,控件可能仍会出现,但不会绘制在任何可触摸区域内。
关于objective-c - 在 UITableView 的头部添加一个 UISegmentedControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11348837/