如何在单击时更改 TableView Header 按钮图像。需要 Objective c 中的代码

How Can I Change TableView Header Button Image On Click. Need code in Objective c

这是我的代码,只是添加了图像,但在按下按钮时没有改变。 请建议一个工作代码。

-(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;
}

首先,按钮已经有一个你可以使用的图像视图,所以不要这样做:

[btn addSubview:imgArrow];

相反,这样做:

[btn setImage:[UIImage imageNamed:@"arrow.jpeg"] forState:UIControlStateNormal];

然后,在您的 sectionOpenToggle 方法中(假设它看起来像这样),更改图像:

- (void)sectionOpenToggle:(UIButton *)sender {
    [sender setImage:[UIImage imageNamed:@"other-arrow.jpeg"] forState:UIControlStateNormal];
    // whatever else you want to do
}