This is my code, just added the image but not changing when hit the button. Please suggest a working code.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
const CGRect fr = CGRectMake(0, 0, 320.0, 40.0 );
btn = [[UIButton alloc]initWithFrame:fr];
UIImageView *imgArrow = [[UIImageView alloc]initWithFrame:CGRectMake(380, 2.5, 25, 25)];
imgArrow.image=[UIImage imageNamed:@"arrow.jpeg"];
[btn setTitle:[self tableView:tableView titleForHeaderInSection:section] forState:UIControlStateNormal ];
btn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;
[btn setBackgroundColor:[UIColor redColor]];
[btn setTag:section];
[btn addSubview:imgArrow];
[btn addTarget:self action:@selector(sectionOpenToggle:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
CodePudding user response:
First, buttons already have an image view that you can use, so don't do this:
[btn addSubview:imgArrow];
Instead, do this:
[btn setImage:[UIImage imageNamed:@"arrow.jpeg"] forState:UIControlStateNormal];
Then, in your sectionOpenToggle
method (assuming it looks like this), change the image:
- (void)sectionOpenToggle:(UIButton *)sender {
[sender setImage:[UIImage imageNamed:@"other-arrow.jpeg"] forState:UIControlStateNormal];
// whatever else you want to do
}