objective-c - 在 UITableView 的头部添加一个 UISegmentedControl

标签 objective-c ios uitableview uisegmentedcontrol

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

相关文章:

iOS - 调用方法 : [self. navigationController pushViewController : animated:], 屏幕全黑

ios - 加载图像后调整 UITableViewCell 的大小

ios - uitableviewcell imageview 在选择时调整大小(仅水平)

ios - '位置管理器(_ :didRangeBeacons:inRegion: )' conflicts with optional requirement method ' locationManager(_:didRangeBeacons:inRegion:)' in protocol

objective-c - 从 CoreData 检索到的 TableView 上的图像

iphone - 如何设置渐变 UITableViewCell 背景?

c# - Xamarin CarouselView ios空白页

ios - 当 react native 应用程序 View 加载时,如何在 native ios 上调用 ViewController 类?

ios - 第二次调用 textFieldDidEndEditing 时 NSFilemanager 不保存文件

ios - 检索带有边距的 UITableViewCell 的框架