2009-05-16 15 views
5

Estoy sobrecargar el método delegado -tableView:heightForRowAtIndexPath: y el uso de -sizeWithFont:constrainedToSize: para establecer mediante programación la altura de la celda, basándose en el texto en la celda:Restablecimiento de encargo UITableViewCell altura cuando el iPhone se gira

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    switch (indexPath.section) { 
    case(kAboutViewOverviewSection): { 
     switch (indexPath.row) { 
     case(kAboutViewOverviewSectionRow): { 
      NSString *text = NSLocalizedString(@"kAboutViewOverviewSectionFieldText", @""); 
      CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(280, 500)]; 
      return s.height + 13; 
     } 
     default: { 
      break; 
     } 
     } 
     break; 
    } 
    default: { 
     break; 
    } 
    } 
    return 44.0; 
} 

Esto funciona cuando la mesa primero se dibuja. Sin embargo, cuando cambio la orientación del dispositivo, de vertical a horizontal, la altura de la celda no cambia.

me trataron reemplazando el método -shouldAutorotateToInterfaceOrientation: en mi controlador de vista de tabla:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    [self.tableView reloadData]; 
    return YES; 
} 

Esto debería volver a cargar los datos de la tabla y (presumiblemente) volver a dibujar las vistas de celda de tabla. Pero esto no tiene ningún efecto.

¿Hay alguna manera de forzar a una celda a volver a dibujar con una nueva altura cuando se gira el dispositivo?

EDITAR: He intentado lo siguiente, a ningún efecto:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    [self.tableView reloadData]; 
} 

Esto es lo que la aplicación se ve como en orientación vertical:

Portait

Esto es lo que la aplicación se ve como en orientación horizontal:

Landscape

Hay un espacio entre los bordes superior e inferior del contenido.

EDIT 2:

Ajuste del parámetro de tamaño a través de la anchura de la estructura de la mesa ayudaron a solucionar este problema de visualización:

CGSize s = [text sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width,500)]; 

Respuesta

6

Es inferior a la óptima, pero la solución más simple es llamar -reloadData en el mesa.

Una solución mejor sería llamar -beginUpdates, -deleteRowsAtIndexPaths:withRowAnimation:, -insertRowsAtIndexPaths:withRowAnimation: y -endUpdates o simplemente -reloadSections:withRowAnimation: si la orientación 3.0 sólo. Esto agregará animación.

Editar: Y usted también necesitará una adecuada tableView:heightForRowAtIndexPath:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    CGSize textSize = [[self textForRowAtIndexPath:indexPath] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake([tableView frame].size.width - 20, 500)]; 
    return textSize.height + 13.0f; 
} 

(donde textForRowAtIndexPath: es un método que devuelve el texto de la celda)

+0

Probé -reloadData. –

+0

Intenté -reloadSections: withRowAnimation: en el método -shouldAutorotateToInterfaceOrientation: y esto tampoco funciona. –

+0

Todo esto funciona si se llama después de que la vista ha sido redimensionada. Eso significa que deben estar en su estructura didRotateFromInterfaceOrientation: – rpetrich

Cuestiones relacionadas