2010-04-17 3 views

Répondre

23

Je ne suis pas sûr à 100% que cela fonctionnera pour un en-tête de table, mais cela fonctionne pour les lignes de table, donc ça vaut le coup. J'ai une variable d'instance headerHeight initialement à 44,0 et je change comme si:

- (void)changeHeight { 
    [self.tableView beginUpdates]; 
    headerHeight = 88.0; 
    [self.tableView endUpdates]; 
} 

Dans mon code Je reviens le headerHeight en heightForRowAtIndexPath mais vous pouvez l'essayer dans heightForHeaderInSection:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return headerHeight; 
} 
+0

grâce @ prendio2 qui ont bien fonctionné. Il n'a pas été en mesure d'enlever la vue (mais c'est ce que j'essayais de faire). mais dès que j'ai défilé il a été enlevé, donc c'est ok :) – jasongregori

+1

pour tous ceux qui ont le même cas d'utilisation que moi (ils veulent supprimer la vue), vous pouvez garder une référence et utiliser la méthode 'removeFromSuperview' sur pendant l'animation pour le cacher correctement. – jasongregori

+3

Cela fonctionne bien, mais la zone qui apparaît couvre mon premier UITableViewCell. Des idées? Un moyen de déplacer la table vers le bas lorsque cela se produit? – arooo

-1

Je n'ai pas testé, mais parfois j'obtenir des animations indésirables quand mes UITableViewCells changer la hauteur. La cause en est que je dessine mes propres cellules, et j'utilise CALayers pour le faire. Dans (void)layoutSubviews de la cellule, je change la taille de mon CALayer être la taille du cadre de la cellule

myLayer.frame = self.bounds; 

Lorsque le cadre/bornes propriété d'un changement de CALayer, il est animé. Donc, en théorie, je dirais que vous pourriez utiliser la méthode tableView: viewForHeaderInSection: qui vous permettrait de dessiner votre propre en-tête de section. Vous pouvez simplement retourner un UIView qui implémente (void)layoutSubviews puis dans cette méthode ne

self.layer.frame = self.bounds; 

Juste une idée.

11

Cela fonctionne :

flatHeader = YES; 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; 
[[self tableView] beginUpdates]; 
[[self tableView] endUpdates]; 
CGRect frame = [[self headerView] frame]; 
frame.size.height = [self tableView:[self tableView] heightForHeaderInSection:0]; 
[[self headerView] setFrame:frame]; 
[UIView commitAnimations]; 
2

Ma solution à Swift:

class MyTableViewController: UITableViewController { 

var sectionHeaderView:UIView? 

...

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 

    sectionHeaderView = UIView(frame: CGRectMake(0, 0, self.tableView.frame.size.width, 30)) 
    sectionHeaderView?.backgroundColor = UIColor.grayColor() 

    var button = UIButton(frame: CGRectMake(0, 0, self.tableView.frame.size.width, 30)) 
    button.backgroundColor = UIColor.darkGrayColor() 
    button.setTitle("collapse/expand", forState: .Normal) 
    button.addTarget(self, action: "collapseOrExpandSectionHeader", forControlEvents: .TouchUpInside) 

    sectionHeaderView?.addSubview(button) 

    return sectionHeaderView 
} 

...

override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 

    if let sectionHeader = sectionHeaderView { 
     return view.frame.size.height 
    } else { 
     return 30.0 
    } 
} 

...

func collapseOrExpandSectionHeader() { 

    if let sectionHeader = sectionHeaderView { 

     let headerHeight:CGFloat 

     if sectionHeader.frame.size.height == 200 { 
      headerHeight = 30.0 
     } else { 
      headerHeight = 200.0 
     } 

     UIView.animateWithDuration(0.3, animations: { 
      self.tableView?.beginUpdates() 
      sectionHeader.frame.size.height = headerHeight 
      self.tableView?.endUpdates() 
     }) 
    } 
} 
Questions connexes