代码之家  ›  专栏  ›  技术社区  ›  Nahum G

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

  •  1
  • Nahum G  · 技术社区  · 10 年前

    我决定在我的应用程序中使用一个表头视图来保存一个搜索栏和一个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;
    }
    

    这会产生良好的结果:

    image

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

    2 回复  |  直到 10 年前
        1
  •  1
  •   Dejan Agostini    10 年前

    标题视图的高度为65点。您在Y=65处插入搜索条,使其超出标头矩形的边界。将搜索栏移动到Y=0,并将分段控件移到其下方,它将正常工作。

    祝你今天愉快:)

        2
  •  0
  •   Lithu T.V    10 年前

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

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

    别忘了设置桌面高度。

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

    以及 <UITableviewDatasource> 在头文件中,指示类实际上正在将数据源方法添加到该表

    A must read doc