невозможно повторно отобразить измененный текст headerView в UITableViewController - PullRequest
0 голосов
/ 19 июля 2011

Мне не удалось выяснить, как снова отобразить мой измененный текст в моем headerView в моем UITableViewController.Я делаю «removeFromSuperview», и он действительно удаляется, но затем он не отображает новый headerView после того, как я повторно добавляю его в мой subView.Вот мой код:

    - (UIView *)headerView
    {
    if (headerView){
        [headerView removeFromSuperview];
        [headerView release];
    }
    UIButton *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;
        [self headerView];
    }  
    else
    {
        displayOnly = YES;
        [self headerView]; 
    }
}

Ответы [ 4 ]

0 голосов
/ 19 июля 2011

Я думаю, что вам нужно вызвать данные перезагрузки таблицы, тогда она будет работать.

0 голосов
/ 19 июля 2011

Вы пытались вызвать [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]; этот вызов.

0 голосов
/ 19 июля 2011

В этом коде:

if (headerView){
    [headerView removeFromSuperview];
    [headerView release];
}

вы удаляете headerView из суперпредставления, но после создания нового headerView не добавляете его в суперпредставление.

0 голосов
/ 19 июля 2011

Вы пытались отладить свое приложение? Где вы добавляете headerView в таблицу? вам нужно изменить код следующим образом

- (void) switch
{
    if (displayOnly == YES)
    {
        displayOnly = NO;
        self.tableView.tableHeaderView = [self headerView];    
    }  
    else
    {
        displayOnly = YES;
        self.tableView.tableHeaderView = [self headerView];    
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...