Pergunta

Estou tendo problemas para animar um cabeçalho de seção de UitableView personalizado.
O objetivo era criar seções colapsáveis.
Quando eu toco no cabeçalho personalizado na primeira vez em que se anima conforme o esperado, no entanto, sempre que deixa uma duplicata no local original e anima outra.

Exemplo de imagem:

alt text alt text
Meu cabeçalho personalizado:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
       UIView* customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease];
       customView.backgroundColor = [UIColor lightGrayColor];

       UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
            headerLabel.backgroundColor = [UIColor clearColor];
            headerLabel.opaque = NO;
            headerLabel.textColor = [UIColor darkGrayColor];
            headerLabel.font = [UIFont boldSystemFontOfSize:16];
            headerLabel.frame = CGRectMake(10, 7, 260.0, 44.0);
            headerLabel.textAlignment = UITextAlignmentCenter;
            NSDictionary *dictionary = [self.data objectAtIndex:section];
            headerLabel.text = [dictionary objectForKey:@"Title"];

            [customView addSubview:headerLabel];


            // add button to right corner of section
        UIButton* headerButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
            headerButton.center = CGPointMake( 160.0, 22.0);
            headerButton.backgroundColor = [UIColor clearColor];
            headerButton.tag = section;
            [headerButton   addTarget:self action:@selector(expandSection:) forControlEvents:UIControlEventTouchUpInside];

            [customView addSubview:headerButton];

            return customView;
}

Meu método de animação:

- (void) expandSection:(id)sender {

    if (expandedSection == [sender tag]) {
        expandedSection = -1;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else if (expandedSection == -1){
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        [self.tableView beginUpdates];  
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates]; 

    }
    //[self.tableView reloadData];
}

Não tenho certeza de que o que está acontecendo, mas as instâncias sugerem que eu preciso negociar alguma coisa. Eu tentei algumas coisas, mas não consigo descobrir isso. Qualquer pessoa ajuda nisso seria ótima!

EDIT: Acredito que o problema é que o recarregamento está causando a visualização personalizada à instância. Não posso lançar a visualização porque preciso dela como uma referência para fazer a atualização da animação. Alguma idéia do que posso fazer para consertar isso?

Foi útil?

Solução

Solução encontrada.

A tabela precisava ser recarregada antes de cada alteração. Dessa forma, a tabela está no estado mais recente antes de fazer alterações.

adicione [self.tableView relloaddata]; como a entrada do punho no método "ExpandSection".

CÓDIGO:

- (void) expandSection:(id)sender {

  [self.tableView reloadData];

    if (expandedSection == [sender tag]) {
        expandedSection = -1;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else if (expandedSection == -1){
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        [self.tableView beginUpdates];  
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates]; 

    }
    //[self.tableView reloadData];
}

Outras dicas

Eu tive um problema semelhante causado pelo uso de células dinâmicas de altura. Eu tinha uma exibição expansível de cabeçalho personalizado e quando estava atualizando a TableView para inserir e remover as fileiras associadas da seção (o que significa que elas estavam se expandindo, respectivamente em colapso), o cabeçalho da seção que era uma subclasse de UITableViewHeaderFooterView não foi reciclado. Então, basicamente, um novo foi alocado e adicionado sobre o antigo, resultando em vistas sobrepostas. O identificador de células foi definido corretamente, então deve ter sido outra coisa. Quando eu removi tableView.sectionHeaderHeight = UITableViewAutomaticDimension e implementado func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat A visualização foi reciclada corretamente e apenas uma visualização do cabeçalho foi exibida para cada seção.

Outra solução que eu realmente trabalhei foi usar UITableViewCell ao invés de UITableViewHeaderFooterView E quando você volta func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? você apenas return cell.contenView, isso funcionará porque o método exige devolver um uiview e desde contentView do uabableViewCell é uma visão que funciona muito bem. A idéia por trás é usar o mecanismo de reciclagem do UitableView através do UitableViewCell e apenas retornar seu conteúdo depois de configurá -lo.

Conclusão. O problema que é muito possível ser causado por UITableViewHeaderFooterView Quando é usado com células de tabela de tabela de dimensionamento e UITableViewAutomaticDimension em vez de calcular manualmente a altura da célula.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top