Reputation: 400
I have set header back ground using the code below but I want to change the color of header back again on run time when I click on the button that is added as a subview on the header. please provide me code, thanks in advance :)
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, _tableView.bounds.size.width, 50)];
header.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"homeCellHeaderBackGround.png"]];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
label.text = _array[section][@"name"];
label.textColor = [UIColor whiteColor];
[header addSubview:label];
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(230, 10, 70, 35)];
[button addTarget:self action:@selector(onExpandButton:) forControlEvents:UIControlEventTouchUpInside];
//[button setTitle:@"Expand" forState:UIControlStateNormal];
[button setTag:section];
[header addSubview:button];
return header;
}
Upvotes: 1
Views: 363
Reputation: 101
You can change the background color of the section header by following code:
- (IBAction)onExpandButton:(id)sender {
UIView *header = ((UIButton *)sender).superView;
header.backgroundColor = <Whatever UIColor you like>;
...
}
Upvotes: 0
Reputation: 4550
You can modify it in run time by:
first:
you can declare a global variable/propery like:
@property (nonatomic) UIView *tableHeader;
and set it under -(void)viewDidLoad
like
self.tableView.tableHeaderView = self.tableHeader;
or using the delagate:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
return self.tableHeader;
}
and modify it anywhere you like, but dont forget to reload you table section header, probably [tableView reloadSections:NSIndexSet withRowAnimation:UITableViewRowAnimation];
will do.
But
In your case you can retreive/get the tableHeader made inside the delegate:
-(UIView *)tableView:tableView viewForHeaderInSection:section
by
UIView *tableHeaderView = [self tableView:yourTableView viewForHeaderInSection:yourSection];
then modify tableHeaderView
..
or simply reassign tableHeader by:
yourTableView.tableHeaderView = tableHeaderView;
your by using header
alone, since as i can see it's a global variable..
change/update it directly like:
header.backgroundColor = [UIColor yourNewColor];
hope i've helped you.. happy coding, cheers..
Upvotes: 1
Reputation: 49
-(IBAction)onExpandButton:(UIButton *)sender { UIView *tmpView = [self.YourTableViewName headerViewForSection:sender.tag]; tmpView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"homeCellHeaderBackGround.png"]]; }
-You got the view from your table view header for section method. After you can set your image view on view.
Upvotes: 0