问题
I've added a button to section header which shows a view on clicking it. My need is that I've to show an "Up-arrow" image while view is shown and "Down-arrow" image when view is hidden.
How can I achieve this? Please help me ...
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 316, 60)];
[btn setTag:section];
[aView addSubview:btn];
[btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown];
btn.backgroundColor=[UIColor clearColor];
}
回答1:
I will suggest that you can have 2 images - 1 for 'Up' and 1 for 'Down' arrow. Set default image to the btn for state UIControlStateNormal and set the other image for state UIControlStateSelected. Now in your sectionTapped: method just change the state of the button. So when you show or hide your view you need to set the button selected YES/NO.
Hope this helps.
回答2:
I had done this before in one of my app.
Take this code as Reference
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = @"1";
[sender setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = @"0";
[sender setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:@selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = @"0";
}
else
{
[button setImage:[UIImage imageNamed:@"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = @"1";
}
回答3:
You can trying using BOOL to check if the button has been pressed or not and animate to rotate the image in your button to point up or down.
-(IBAction)dropdownBtnClicked:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
if (!isDropdownEnabled)
{
//BOOL - checking if the button has been pressed or not
isDropdownEnabled=TRUE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(270/180*M_PI);
}
else
{
isDropdownEnabled=FALSE;
drpDwnBtn.transform=CGAffineTransformMakeRotation(0);
}
[UIView commitAnimations];
}
来源:https://stackoverflow.com/questions/16229910/how-to-change-button-image-added-to-tableview-section-header