Ios Can';无法在Iphone中访问标题视图

Ios Can';无法在Iphone中访问标题视图,ios,iphone,objective-c,uitableview,Ios,Iphone,Objective C,Uitableview,我在TableView中添加了一个header视图,但我无法访问该视图或iAction方法中header视图的任何子视图 我的代码如下: -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *header_View=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)]; header_

我在TableView中添加了一个header视图,但我无法访问该视图或
iAction
方法中header视图的任何子视图

我的代码如下:

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *header_View=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)];
    header_View.tag=section;
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.Frame=header_View.bounds;
    [btn setTitle:[arr_headerTitles objectAtIndex:section] forState:UIControlStateNormal];
    btn.titleLabel.font=FontChampagneLimousinesBold(18);
    btn.tag=section;
    [btn addTarget:self action:@selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *img_arrow=[[UIImageView alloc]initWithFrame:CGRectMake(150, 05, 15, 15)];
    img_arrow.image= [UIImage imageNamed:@"arrow_white_down.png"];

    [header_View addSubview:btn];
    [header_View addSubview:img_arrow];
    return header_View;
}

-(void)expandTableHeaderView:(UIButton*)sender{
     //  UIView* _header_view =(UIView*)[_tbl viewWithTag:[sender tag]]; //Also tried
        UIView *_header_view=[_tbl headerViewForSection:section]; //shows nil;

        UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]];
}
帮我解决这个问题 *谢谢你*

试试这个:

-(void)expandTableHeaderView:(UIButton*)sender{
    UIView *_header_view =  sender.superview;
    // ...
}

在创建标题视图时,尝试使用UITableViewHeaderFooterView而不是UIView,这样似乎可以正常工作

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView *header_View=[[UITableViewHeaderFooterView alloc]initWithFrame:CGRectMake(0, 0, 160, 40)];
    header_View.tag=section;
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.Frame=header_View.bounds;
    [btn setTitle:[arr_headerTitles objectAtIndex:section] forState:UIControlStateNormal];
    btn.titleLabel.font=FontChampagneLimousinesBold(18);
    btn.tag=section;
    [btn addTarget:self action:@selector(expandTableHeaderView:) forControlEvents:UIControlEventTouchUpInside];
    UIImageView *img_arrow=[[UIImageView alloc]initWithFrame:CGRectMake(150, 05, 15, 15)];
    img_arrow.image= [UIImage imageNamed:@"arrow_white_down.png"];

    [header_View addSubview:btn];
    [header_View addSubview:img_arrow];
    return header_View;
}

-(void)expandTableHeaderView:(UIButton*)sender{
     //  UIView* _header_view =(UIView*)[_tbl viewWithTag:[sender tag]]; //Also tried
        UITableViewHeaderFooterView *_header_view=[_tbl headerViewForSection:sender.tag]; //shows nil;

        UIView *view = (UIView *)[_header_view viewWithTag:[sender tag]];
}

我想你的头顶高度没有定义。 试试这个:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   return 40.0;//the height of your header view
}
这可能有用


请在iAction方法中添加断点标记,以查看其是否有效。

能否添加标记的一个偏移量?例如,定义int offset=500;当页眉_View.tag=偏移量+截面时;然后在动作[_TBLVIEWWITHTAG:(偏移+部分)]中,请检查您的tableview委托连接tableview中是否显示了您的headerview?