不会为UITableView的第0部分的标题视图调用GestureRecognizer或Button Action

时间:2014-09-05 12:48:44

标签: 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;
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,它显示在可见的NavigationBar下方,但按钮操作正常。但是,如果yAxis = 0,则在NavigationBar下方可以正确显示,但是没有发生与0部分标题的触摸交互。所以这里有一个两难的问题。

问题应该是什么?

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

1 个答案:

答案 0 :(得分:0)

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