Add a button section Header?

10,697

Solution 1

Try like this May be it helps you,

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //Headerview
    UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)] autorelease];
     UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)];
    button.tag = section;
    button.hidden = NO;
    [button setBackgroundColor:[UIColor clearColor]];
    [button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown];
    [myView addSubview:button];
      return myView;
}

you can change the height of the header section here

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}

for getting same Button image you can take your custom image ,by using default image you can't show like that.

Solution 2

yes it is possible.
for that you have to create your custom View with UIButton and return from the UITableView Delegate method:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 ///your implementation
  return customView;
}

make your view height 45 instead of 20. its too small.

UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)];

Solution 3

The problem in your code is the button frame is (275.0, 5.0, 30.0, 30.0), but the myView frame is (0.0, 0.0, 300.0, 20.0). The height of the button is 30, but myView is 20 only. The Y position of the button is 5, so the myView height needs to be 40.

change the myView frame as (0.0, 0.0, 300.0, 40.0).

And the height of the section also set to 40 using heightForHeaderInSection: delegate method.

Simply follow this steps....

  1. set myView frame as (0.0, 0.0, 300.0, 40.0).
  2. set button frame as (275.0, 5.0, 300.0, 20.0).
  3. set the height of the section as 40 using heightForHeaderInSection delegate method.

This will solve your problem.

Share:
10,697
Rey_mysterio
Author by

Rey_mysterio

Updated on June 04, 2022

Comments

  • Rey_mysterio
    Rey_mysterio almost 2 years

    I have a tableView with two section I want to add a button in section header(Action Button) It is possible to do same in the sample image?

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if(section ==0)
    {
        return 0;
    }
    else{
        UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [button setFrame:CGRectMake(275.0, 5.0, 30.0, 30.0)];
        button.tag = section;
        button.hidden = NO;
        [button setBackgroundColor:[UIColor clearColor]];
        [button addTarget:self action:@selector(insertParameter:) forControlEvents:UIControlEventTouchDown];
        [myView addSubview:button];
        return myView;    }
    }
    

    enter image description here