IPhone: Cómo desplazarse hasta la primera celda de la segunda sección, dejando la cabecera de la primera sección visible

StackOverflow https://stackoverflow.com/questions/4597839

Pregunta

Tengo una UITableView con filas y secciones. Me gustaría ir al primer elemento de la segunda sección, dejando la cabecera de la primera sección visible. Al igual que si hubiera desplazado manualmente la lista hasta llegar a ese estado.

---- TOP OF SCREEN ----
Header of first section
Header of the second section
cell 1
cell 2
cell 3
Header of the third section
cell 1
cell 2
...

scrollToRowAtIndexPath: [NSIndexPath indexPathForRow: 0 insection: 1] no hacer el trabajo, se esconde la cabecera de la primera sección.

¿Fue útil?

Solución

Nos estamos moviendo. He encontrado este método basado en la idea de Kevin. Para ser capaz de establecer animado a SÍ, ver el final de la animación usando un método delegado de UIScrollView. Funciona. Sin embargo, cualquier solución que ayudaría a no hacer animaciones 2 sería muy apreciada. Cualquier idea acerca de cómo hacer esto?

- (IBAction) scrollToToday:(BOOL)animate {
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionTop animated:animate];
    if (animate == NO) [self showFirstHeaderLine:NO];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self showFirstHeaderLine:YES];
}

- (void) showFirstHeaderLine:(BOOL)animate {
    CGRect headerRect = [self.tableView rectForHeaderInSection:1];
    CGPoint scrollPoint = headerRect.origin;
    scrollPoint.y -= headerRect.size.height;
    [self.tableView setContentOffset:scrollPoint animated:animate];
}

El tipo de este código, el proceso, cuando está inspirado se ajusta en SÍ debe reproducirse infinitamente beetween scrollViewDidEndScrollingAnimation y showFirstHeaderLine ... Es bucles, sí, pero sólo una vez ... ¿Alguna idea de por qué?

Otros consejos

Se puede agarrar el rect para la fila que desea, luego restar la altura de la cabecera de la sección anterior y desplazamiento a ese punto. Algo así como lo siguiente (no probado) debería funcionar:

CGRect rowRect = [table rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
CGRect headerRect = [table rectForHeaderInSection:0];
rowRect.origin.y -= headerRect.size.height;
rowRect.size.height += headerRect.size.height;
[table scrollRectToVisible:rowRect animated:YES]; // UITableView is a subclass of UIScrollView

He intentado su código, y funciona !!

Para la pregunta de bucle, ya que se está configurando un desplazamiento (SetContentOffset), no tiene nada que ver con el desplazamiento. Es no va a llamar ScrollView delegado. Por lo que el scrollViewDidEndScrollingAnimation se llamará sólo una vez, que ha sido llamado desde scrollToRowAtIndexPath.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top