首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法与表视图标题中的控件交互

无法与表视图标题中的控件交互
EN

Stack Overflow用户
提问于 2014-08-06 21:07:23
回答 2查看 470关注 0票数 1

我决定在我的应用程序中使用一个表格标题视图来保存一个搜索栏和一个UISegmentedControl。以下是视图控制器的viewDidLoad

代码语言:javascript
复制
- (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,但问题仍然存在。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-06 22:01:00

标题视图的高度为65点。您正在将搜索栏插入Y=65,因此超出了标头rect的范围。将你的搜索栏移动到Y=0,并将你的分段控件移到它下面,它就能正常工作了。

祝你今天愉快:)

票数 1
EN

Stack Overflow用户

发布于 2014-08-06 21:12:43

USe委托方法来设置headerview.Create并在此方法中返回头视图。

代码语言:javascript
复制
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

不要忘记设置表格的标题高度。

代码语言:javascript
复制
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

以及头文件中的<UITableviewDatasource>,以指示该类实际上拥有该表的数据源方法。

一读必读医生

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25170218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档