Question

Je ne parviens pas à animer un en-tête de section UITableView personnalisée.
L'objectif était de créer des sections collapsable.
Quand je tape sur l'en-tête personnalisé la première fois qu'il anime comme prévu, mais à chaque fois après qu'il laisse un double dans l'emplacement d'origine et anime une autre.

Image Exemple:

text alt     
   Mon-tête personnalisé:

- (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;
}

Mon animation Méthode:

- (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];
}

Je ne sais pas exactement ce qui se passe, mais les cas suggère que je dois dealoc quelque chose. J'ai essayé quelques petites choses mais je ne peux pas comprendre cela. aide Toute personne sur ce serait génial!

Edit: Je crois que le problème est que reloadSections est à l'origine de la vue personnalisée à l'instance. Je ne peux pas libérer la vue parce que je besoin comme référence pour faire la mise à jour de l'animation. Toutes les idées sur ce que je peux faire pour résoudre ce problème?

Était-ce utile?

La solution

Solution trouvée.

Le tableau devait être rechargées avant chaque changement. De cette façon, la table est au dernier état avant toute modification.

add [Self.tableView reloadData]; que l'entrée de poing dans le procédé « expandSection ».

CODE:

- (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];
}

Autres conseils

J'ai eu un problème similaire qui a été causée par l'utilisation de cellules de hauteur dynamiques. J'ai eu une vue extensible en-tête personnalisé et quand je mettais tableView pour insérer et retirer les lignes associées de la section (ce qui signifie qu'ils se développaient, effondrement respectivement), l'en-tête de section qui était une sous-classe de UITableViewHeaderFooterView n'a pas été recyclé. Donc, fondamentalement, un nouveau a été alloué et ajouté sur l'ancien résultat vues qui se chevauchent. L'identifiant de cellule a été correctement réglée de façon doit avoir été autre chose. Quand j'ai enlevé tableView.sectionHeaderHeight = UITableViewAutomaticDimension et mis en œuvre func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat la vue se est correctement recyclé et une seule vue d'en-tête a été affiché pour chaque section.

Une autre solution que je me suis tourné vers réellement le travail était d'utiliser UITableViewCell au lieu de UITableViewHeaderFooterView et lorsque vous revenez à func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? que vous venez return cell.contenView, cela fonctionne parce que la méthode nécessite de retourner un UIView et depuis le contentView du UITableViewCell est un UIView il fonctionne très bien. L'idée est d'utiliser le mécanisme de recyclage des UITableView par UITableViewCell et juste retourner son contenu après avoir configuré derrière.

Conclusion. Le problème, il est très possible d'être causée par UITableViewHeaderFooterView quand est utilisé avec l'auto dimensionnement des cellules tableView et UITableViewAutomaticDimension au lieu de calculer manuellement la hauteur de la cellule.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top