为什么UItableView中的按钮没有响应?

时间:2014-06-03 07:58:21

标签: uitableview uibutton

我有一个带有多个部分的UItableView,在每个部分中,我都有一个包含按钮的视图,并且在每个部分中都有一行,但我已经隐藏了该行。

简而言之,我的tableview是一个部分的集合,它们依次包含每个部分的按钮,当我点击按钮时它没有响应但是一旦我滚动,我的表从下到上按钮被响应。这是什么问题?

谢谢!

以下是代码:

  - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  {
    sheettable.scrollsToTop=YES;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)];
    CGRect rect= CGRectMake(0, 0, 320,40);
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:(UIRectCornerAllCorners) cornerRadii:CGSizeMake(5.0, 5.0)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame =rect;
    maskLayer.path = maskPath.CGPath;
    headerView.layer.mask = maskLayer;
    headerView.backgroundColor=[UIColor greenColor];
    UITextField *label1 = [[UITextField alloc] init] ;
    label1.delegate=self;
    label1.frame = CGRectMake(5, 1, 200, 35);

    label1.font = [UIFont boldSystemFontOfSize:16];
    label1.text = [[result2 objectAtIndex:section] objectForKey:@"cat_desc"];
    UIButton *btn=[[UIButton alloc]init];
    btn.frame=CGRectMake(5, 1, 200, 35);
    btn.tag=section;
    btn.backgroundColor=[UIColor clearColor];
    [headerView addSubview:label1];
    [btn addTarget:self action:@selector(selectedsections) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:btn];
    cameracategory=section;
    return headerView;

 }

 -(void)selectedsections
  {

    if(cameracategory==0)
    {
        NSLog(@"At Index");
    }
    if(cameracategory==1)
    {
        NSLog(@"At Index");
    }
    if(cameracategory==2)
    {
        NSLog(@"At Index");
    }
    if(cameracategory==3)
    {
        NSLog(@"At Index");
    }
    if(cameracategory==4)
    {
        NSLog(@"At Index");
    }

 }

1 个答案:

答案 0 :(得分:0)

我测试了你的代码,它确实有效,但我自己创建了UITableView。你应该检查一下。也尝试编写更易读的代码:)将来会更容易:)

你有一些未对齐的帧(子视图比父视图大)。

- (void)sectionButtonAction:(UIButton *)sender {
    NSLog(@"Section button tapped: %i", sender.tag);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect tableRect = tableView.frame;

    CGRect headerRect = CGRectZero;
    headerRect.size.width = tableRect.size.width;
    headerRect.size.height = 22;

    CGRect bezierRect = CGRectZero;
    bezierRect.size.width = tableRect.size.width;
    bezierRect.size.height = 44;

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    [maskLayer setFrame:bezierRect];
    [maskLayer setPath:[UIBezierPath bezierPathWithRoundedRect:bezierRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(5, 5)].CGPath];

    UIView *headerView = [[UIView alloc] initWithFrame:headerRect];
    [headerView setBackgroundColor:[UIColor greenColor]];
    [[headerView layer] setMask:maskLayer];

    CGRect labelRect = CGRectZero;
    labelRect.origin.x = 5;
    labelRect.origin.y = 1;
    labelRect.size.width = 200;
    labelRect.size.height = 35;

    UILabel *headerLabel = [[UILabel alloc] initWithFrame:labelRect];
    [headerLabel setFont:[UIFont boldSystemFontOfSize:16]];
    [headerLabel setText:@"asd"];
    [headerView addSubview:headerLabel];

    CGRect buttonRect = labelRect;

    UIButton *headerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [headerButton setFrame:buttonRect];
    [headerButton setTag:section];
    [headerButton addTarget:self action:@selector(sectionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:headerButton];

    return headerView;
}