【问题标题】:Can't Interact With Controls In Table View Header无法与表视图标题中的控件交互
【发布时间】:2014-09-29 23:15:20
【问题描述】:

我决定在我的应用程序中使用表头视图来保存搜索栏和 UISegmentedControl。这是视图控制器的viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 65, 320, 44)];
    [self.searchBar sizeToFit];
    [self.searchBar setUserInteractionEnabled:YES];
    [self setSearchController:[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]];

    self.mainSegment = [[UISegmentedControl alloc] initWithItems:@[@"YouTube", @"iTunes"]];
    [self.mainSegment setFrame:CGRectMake(8, 109, 305, 29)];
    [self.mainSegment setSelectedIndex:0];
    [self.mainSegment addTarget:self action:@selector(searchTypeChanged:) forControlEvents:UIControlEventValueChanged];
    [self.mainSegment setUserInteractionEnabled:YES];

    UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 65)];
    [headerView setBackgroundColor:[UIColor clearColor]];
    [headerView addSubview:self.searchBar];
    [headerView addSubview:self.mainSegment];
    [headerView bringSubviewToFront:self.searchBar];
    [headerView bringSubviewToFront:self.mainSegment];
    [headerView setUserInteractionEnabled:YES];

    self.tableView.tableHeaderView = headerView;
    self.tableView.userInteractionEnabled = YES;
}

这会产生一个很好的结果:

但是,我无法与搜索栏或分段控件交互。如上所示,我尝试将 userInteractionEnabled 设置为 YES,但问题仍然存在。有什么想法吗?

【问题讨论】:

  • 无响应的控件通常是因为它们超出了它们的超级视图的范围。为您的标题视图提供不同的背景颜色,以查看它们是否在边界内。
  • @rdelmar 已经解决了,谢谢!

标签: ios uitableview uisearchbar uisegmentedcontrol


【解决方案1】:

标题视图的高度为 65 磅。您在 Y=65 处插入搜索栏,因此它超出了标题矩形的范围。将搜索栏移动到 Y=0 和下方的分段控件,它会正常工作。

祝你有美好的一天:)

【讨论】:

    【解决方案2】:

    使用委托方法设置headerview。在此方法中创建并返回你的headerview。

    -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    

    别忘了设置表头高度。

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
    

    和头文件中的<UITableviewDatasource> 表明该类实际上具有该表的数据源方法

    A must read doc

    【讨论】:

    • OP 正在制作表格标题视图,而不是部分标题,所以这不是要走的路。
    猜你喜欢
    • 2021-10-26
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多