Вы пытались вызвать [headerView setNeedsDisplay];
после установки нового заголовка для метки вместо того, чтобы каждый раз заново создавать вид?
И может быть, вам даже не нужно это делать вообще. Если вы храните UIButton
и UIView
, если я прав, он автоматически перерисовывается при добавлении нового заголовка. Примерно так:
в вашем .h файле
UIButton* headerLabel;
UIView* headerView;
и в вашем .m файле
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return [self headerView];
}
- (UIView *)headerView{
if(headerView){
return headerView;
}
if(!headerLabel){
headerLabel = [UIButton buttonWithType:UIButtonTypeRoundedRect];
}
if (displayOnly == NO)
[headerLabel setTitle:@"Click here to switch to display only mode." forState:UIControlStateNormal];
else
[headerLabel setTitle:@"Click here to switch to practice session mode." forState:UIControlStateNormal];
float w = [[UIScreen mainScreen] bounds].size.width;
CGRect headerLabelFrame = CGRectMake(8.0, 8.0, w - 16.0, 30.0);
[headerLabel setFrame:headerLabelFrame];
CGRect headerViewFrame = CGRectMake(0, 0, w, 48);
[headerLabel addTarget:self action:@selector(switch) forControlEvents:UIControlEventTouchUpInside];
headerView = [[UIView alloc] initWithFrame:headerViewFrame];
[headerView addSubview:headerLabel];
return headerView;
}
- (void) switch{
if (displayOnly == YES){
displayOnly = NO;
[headerLabel setTitle:@"Click here to switch to display only mode." forState:UIControlStateNormal];
} else{
displayOnly = YES;
[headerLabel setTitle:@"Click here to switch to practice session mode." forState:UIControlStateNormal];
}
//may be there's need to make additional call
[headerView setNeedsDisplay];
}
Таким образом, вы держите один и тот же экземпляр UIView
и UIButton
, только обновляя его заголовок и, возможно, вручную вызывая перерисовку, делая [headerView setNeedsDisplay];
этот вызов.