2011-04-14 30 views
5

Estoy trabajando con una UITableView, con UITableViewCell. Mi aplicación admite la orientación horizontal, así que he creado 2 UITableViewCell: una para el portraid y otra para el paisaje.reloadData en un UITableView cuando deviceOrientation cambia

En mi método cellForRowAtIndexPath este es mi código

static NSString *CellIdentifier; 
static NSString *NibNamed; 

UIDeviceOrientation deviceOrientation = [UIApplication sharedApplication].statusBarOrientation; 

if (deviceOrientation != UIDeviceOrientationLandscapeLeft && 
    deviceOrientation != UIDeviceOrientationLandscapeRight) 
{ 
    CellIdentifier= @"Cell1"; 
    NibNamed = @"cell_news"; 
} 
else 
{ 
    CellIdentifier= @"Cell2"; 
    NibNamed = @"cell_news_landscape"; 
} 

[[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:NULL]; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:NibNamed owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
} 
///here i add title, description, ecc... in my UITableViewCell 

Luego, en shouldAutorotateToInterfaceOrientation i escribió esto:

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

¿Dónde está el problema? Si comienzo la aplicación en vertical, cuando giro el dispositivo por primera vez, no pasa nada (y UITableCellView es la tabla_de_modelo_vuelta). Cuando giro el dispositivo por segunda vez (y estoy en vertical o boca abajo), veo el landscape_table_cell (y, por supuesto, no veo todo el texto porque sale de la pantalla). Así que ... excepto la primera vez, las otras veces que giro el dispositivo, veo el table_cell_view incorrecto !!

¿Sabes por qué? Gracias!

Respuesta

9

Sus datos se vuelven a cargar antes de que el dispositivo cambie de orientación. shouldAutorotateToInterfaceOrientation se llama antes de cualquier rotación.

Prueba el didRotateFromInterfaceOrientation

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
[my_table reloadData]; 
} 

También tenga en cuenta la respuesta dada por Jhaliya, debido a que la solución será mejor que la recarga se tableView. Solo debe volver a cargar la vista de tabla si la fuente de datos ha cambiado.

+0

gracias! ¡¡esto funciona!! – JAA

+0

Pero eche un vistazo a lo que @Jhaliya tiene que decir. Realmente es mejor hacer que sus celdas se ajusten al tamaño de forma automática. – rckoenes

4

No llamar reloadData de shouldAutorotateToInterfaceOrientation:

Utilice la propiedad autoresizingMask UIView tanto para UITableView y UITableViewCell cuando se crea el objeto para ambos. porque UITableView and UITableViewCell are the subclass of UIView`.

@property(nonatomic) UIViewAutoresizing autoresizingMask 
0

Creo que hay dos problemas. 1) shouldAutoRotateToInterfaceOrientation se llama ANTES de que la vista gire, por lo que si actualiza, es demasiado pronto. 2) Creo que necesita administrar el rediseño en lugar de confiar en reloadData. Por lo tanto, anule el método de volver a dibujar de tableViewCell, o configure sus máscaras de aumento de tamaño apropiadamente. Espero que esto ayude. -Mike

0

Si el autosizing no le conviene así, también se puede lograr que las células índices visibles con

- (NSArray *)indexPathsForVisibleRow

Y a continuación, obtener las células en sí con

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

Y llame a un método como

[cell interfaceOrientationDidChangeTo:interfaceOrientation]

Y deje que el trabajo lo realice la propia célula.

Cuestiones relacionadas