开发者

UITableView Sectionheaders each with a button, but just the first Button sends action

开发者 https://www.devze.com 2023-02-17 20:07 出处:网络
i add a button to my Sectionheaders, but only the first Button works. The other Buttons neither show a animation on touc开发者_如何转开发h nor send a action.

i add a button to my Sectionheaders, but only the first Button works.

The other Buttons neither show a animation on touc开发者_如何转开发h nor send a action.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//Headerview
UIView *myView = [[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 20.0)] autorelease];
//HeaderLabel
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 5.0, 300.0, 30.0)] ;
label.textColor = [UIColor colorWithRed:0.286 green:0.341 blue:0.424 alpha:1.];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0, 1);
label.font = [UIFont boldSystemFontOfSize:18];
label.backgroundColor = [UIColor clearColor];

//AddParameterButton
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];

label.text = @"Parameter";
[myView addSubview:label];
[myView addSubview:button];
[myView bringSubviewToFront:button];
[label release];
return myView;
}

Any solving proposals? THANK YOU

//edit:

i debugged a bit and the viewForHeaderInSection: gets called six times but there are only two sections. hope i can help

//edit2

tried to add the buttons as property and do it the nondynamic way, but doesn't help, too


@Seega:

Try to set unique identifiers for each of the buttons buttons.

Basically I am talking about button tags and then try to access the buttons by its tag in the insertParameter: method, I think this would solve your problem.

As you have one button per section, what I feel is that you have best option to set tag as indexPath.section

You can add the tag in - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method as shown below

[button setTag:section];

OR

button.tag = section;

So try using the button tags just to uniquely identify the button over each section.

Hope this helps you


You should create buttons for each section. Use an if condition and and put your code inside each block.

if (section == 0) 
{

//code to create your button

}
else if(section == 1) 
{

//code to create your button

}

else
{

//code to create your button

}

Try this.


had in my heightForHeaderInsection

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section < 2)
    return 40;
return 10;
}

changed it and work fine
so thank you very much, think i'm a little codeblind
sorry and thank you

0

精彩评论

暂无评论...
验证码 换一张
取 消