Rey_mysterio
Rey_mysterio

Reputation: 81

Add a button section Header?

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

Upvotes: 2

Views: 10725

Answers (4)

user2351400
user2351400

Reputation: 1

set the height of the header view

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 81;

}

Upvotes: 0

Bhanu Prakash
Bhanu Prakash

Reputation: 1483

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.

Upvotes: 1

Balu
Balu

Reputation: 8460

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.

Upvotes: 7

KDeogharkar
KDeogharkar

Reputation: 10959

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)];

Upvotes: 1

Related Questions