Reputation: 81
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; }
}
Upvotes: 2
Views: 10725
Reputation: 1
set the height of the header view
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 81;
}
Upvotes: 0
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....
myView
frame as (0.0, 0.0, 300.0, 40.0)
.button
frame as (275.0, 5.0, 300.0, 20.0)
.heightForHeaderInSection
delegate method.This will solve your problem.
Upvotes: 1
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
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