![](/img/trans.png)
[英]How to change image on button by click and hide the next one in 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.