Ios 未为UITableView第0节的标题视图调用手势识别器或按钮操作

Ios 未为UITableView第0节的标题视图调用手势识别器或按钮操作,ios,objective-c,uitableview,uigesturerecognizer,frame,Ios,Objective C,Uitableview,Uigesturerecognizer,Frame,我遇到了一个奇怪的行为,下面是我对sectionHeaderView的实现 - (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 256, 48)]; v.tag = section; v.userInteractionEnabled = YES; UI

我遇到了一个奇怪的行为,下面是我对sectionHeaderView的实现

- (UIView*) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 256, 48)];
v.tag = section;
v.userInteractionEnabled = YES;
UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 24, 24)];

UILabel* l = [[UILabel alloc] initWithFrame:CGRectMake(64, 2, 200, 44)];
l.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0];

l.font = [UIFont fontWithName:@"Ubuntu-Regular" size:16];

v.backgroundColor = [UIColor colorWithRed:242.0f/255.0f green:242.0f/255.0f blue:242.0f/255.0f alpha:1.0];

[v addSubview:l];
[v addSubview:iv];
iv.contentMode = UIViewContentModeScaleAspectFit;

UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.tag = section;
btn.frame = CGRectMake(226, 19, 10, 10);
[btn setImage:[UIImage imageNamed:@"arrow_right.png"] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"arrow_left.png"] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(moreButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:btn];

//--- did some modifications in UI ---///

UITapGestureRecognizer* g = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSectionHeader:)];
[v addGestureRecognizer:g];

UIButton* btnOverall = [UIButton buttonWithType:UIButtonTypeCustom];
btnOverall.frame = CGRectMake(0, 0, 256, 48);
btnOverall.tag = section;
[btnOverall addTarget:self action:@selector(moreButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[v addSubview:btnOverall];
btnOverall.layer.zPosition = MAXFLOAT; // so that button comes to top and is easily clickable.

UIView *singleLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 47, 256, 1)];
singleLineView.backgroundColor = [UIColor lightGrayColor];//[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:1.0];

[v addSubview:singleLineView];
return v;


}
手势识别器的实现方式如下:

- (void) tapSectionHeader:(UITapGestureRecognizer*)g {
self.selectedSection = g.view.tag;
//some code
}
但奇怪的是,无论是tapSectionHeader:还是moreButtonClicked:都没有被要求使用第0节,即“主页”选项。对于其他节标题,它工作得非常好

注意:似乎是tableview框架的问题。以下是我设置tableViewFrame的方式:

CGRect screenSize = [UIScreen mainScreen].bounds;
xAxis = 0;
yAxis = 48;
height = screenSize.size.height;
width = MENU_WIDTH;

self.frame = CGRectMake(-width, yAxis, width, height);
self.backgroundColor = BACKGROUND_COLOR;
if(!sender.navigationController.navigationBarHidden) {
    menuTable = [[UITableView alloc]initWithFrame:CGRectMake(xAxis, yAxis, 256, height) style:UITableViewStylePlain];
}else {
    menuTable = [[UITableView alloc]initWithFrame:CGRectMake(xAxis, yAxis, 256, height) style:UITableViewStylePlain];
}
在这里,如果yAxis=48,它会显示在可见导航栏的下方,但按钮操作可以正常工作。但是,如果yAxis=0,则它在导航栏下方正确可见,但不会与第0节标题发生触摸交互。所以我在这里进退两难

问题应该是什么


注意:navigationBar半透明属性设置为NO。因此,从0开始启动yAxis。

问题得到解决。实际上,这甚至不是一个框架的问题。这是viewcontroller的子视图位于顶部的问题!我将此视图作为子视图添加到,并且在api调用后为其添加另一个视图。但是,在执行view.layer.zPosition=MAXFLOAT时,此视图显示在顶部。但这是在添加另一个视图之前完成的。因此,在添加了所有子视图之后执行此操作会有所帮助