2010-04-17 25 views

Respuesta

23

No estoy 100% seguro de que esto funcionará para un encabezado de tabla, pero funciona para las filas de la tabla, así que vale la pena intentarlo. Tengo una variable de instancia headerHeight establece inicialmente en 44,0 y lo cambio como tal:

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

En mi código que devolver el headerHeight en heightForRowAtIndexPath pero puedes probarlo en heightForHeaderInSection:

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

gracias @ prendio2 que funcionaron bien. sin embargo, no fue capaz de eliminar la vista (que es lo que estaba tratando de hacer). pero tan pronto como me desplacé se eliminó, así que está bien :) – jasongregori

+1

para cualquier persona que tenga el mismo caso de uso que yo (quieren eliminar la vista), puede mantener una referencia y usar el método 'removeFromSuperview' en durante la animación para ocultarlo correctamente. – jasongregori

+3

Esto funciona bien, pero el área que aparece abarca mi primer UITableViewCell. ¿Algunas ideas? ¿Hay alguna manera de cambiar la tabla de abajo cuando esto sucede? – arooo

-1

No he probado esto, pero a veces me da animaciones no deseados cuando mis UITableViewCells cambio de altura. La causa de esto es que dibujo mis propias celdas y uso CALayers para hacerlo. En la celda de (void)layoutSubviews que cambiaría el tamaño de mi CALayer ser el tamaño del marco para la célula

myLayer.frame = self.bounds; 

Cuando la propiedad de marco/límites de un CALayer cambios, está animado. Entonces, en teoría, yo diría que podría usar el método tableView: viewForHeaderInSection: que le permitiría dibujar su propio encabezado de sección. Se podía devolver un UIView que implementa (void)layoutSubviews y entonces en ese método de hacer

self.layer.frame = self.bounds; 

Es sólo una idea.

11

Estos trabajos :

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

Mi solución en 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() 
     }) 
    } 
} 
Cuestiones relacionadas