Pregunta

Tengo una aplicación que funciona de forma similar a cómo funciona la aplicación de contacto de iPhone. Cuando agregamos un nuevo contacto, el usuario se dirige a una pantalla de solo visualización con información de contacto. Si seleccionamos " Todos los contactos " desde la barra de navegación, el usuario se desplaza a la lista de todos los contactos donde se puede ver el contacto recientemente agregado.

Podemos mover la vista a una fila en particular usando:

    [itemsTableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionBottom];

... pero no está funcionando. Llamo a esto justo después de llamar:

    [tableView reloadData];

Creo que no debo llamar al método selectRowAtIndexPath: animated: scrollPosition aquí. Pero si no es aquí, entonces ¿dónde?

¿Hay algún método de delegación que se llame después del siguiente método?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
¿Fue útil?

Solución

Creo que tengo una aplicación similar: una lista de elementos., toca '+' - > pantalla nueva, regrese y vea la lista actualizada, desplácese para mostrar el elemento agregado en la parte inferior.

En resumen, pongo reloadData en viewWillAppear: animated: y scrollToRowAtIndexPath: ... en viewDidAppear: animated: .

// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (dataHasChanged) {
        self.dataHasChanged = NO;
        [[self tableView] reloadData];
    } else {
        self.scrollToLast = NO; // No reload -> no need to scroll!
    }
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (scrollToLast) {
        NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
        [[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
    }
}

Espero que esto ayude. Si agrega algo en medio de la lista, podría desplazarse fácilmente a esa posición.

Otros consejos

Puedes probar esto, mi aplicación es similar a la tuya cuando hago clic en el botón de desplazamiento de uitableview.

[tableviewname setContentOffset:CGPointMake(0, ([arrayofcontact count]-10)*cellheight) animated:YES];

10 es para cuántas celdas quieres desplazarte hacia arriba

Espero que esto te ayude :-)

La animación de desplazamiento es inevitable cuando el desplazamiento se establece desde viewDidAppear (_ :) . Un mejor lugar para configurar el desplazamiento de desplazamiento inicial es viewWillAppear (_ :) . Deberá forzar el diseño de una vista de tabla, porque el tamaño del contenido no está definido en ese momento.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    tableView.setNeedsLayout()
    tableView.layoutIfNeeded()

    if let selectedIndexPath = selectedIndexPath, tableView.numberOfRows(inSection: selectedIndexPath.section) > 0 {
        tableView.scrollToRow(at: selectedIndexPath, at: .top, animated: false)
    }
}

¿Se han agregado suficientes contactos ficticios para probar el desplazamiento? Parece que solo cuando su tableView es de un tamaño determinado, el iPhone encuentra el inputus para desplazarse.

Este es el código que me funciona en mi proyecto. Utilizo un botón anterior y, por lo tanto, desplazo la vista de tabla un poco más hacia abajo, lo que usualmente iría con UITABLEVIEWSCROLLPOSITION. (Mi botón anterior no funcionará si no puede "ver" la celda anterior.

Ignora algunas de las llamadas a métodos personalizados.

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    //Show the navButtons
    [self navButtonAnimation];


    DetailsStandardCell *cell = (DetailsStandardCell *)textField.superview.superview.superview;

    self.parentController.lastCell = cell;


    //Code to scroll the tableview to the previous indexpath so the previous button will work. NOTE previous button only works if its target table cell is visible.
    NSUInteger row = cell.cellPath.row;
    NSUInteger section = cell.cellPath.section;

    NSIndexPath *previousIndexPath = nil;


    if (cell.cellPath.row == 0 && cell.cellPath.section != 0) //take selection back to last row of previous section
    {
    NSUInteger previousIndex[] = {section -1, ([[self.sections objectForKey:[NSNumber numberWithInt:section - 1]]count] -1)};
    previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];   
    }
    else if (cell.cellPath.row != 0 && cell.cellPath.section != 0)
    {
        NSUInteger previousIndex[] = {section, row - 1};
        previousIndexPath = [[NSIndexPath alloc] initWithIndexes:previousIndex length:2];       

    }


    [self.theTableView scrollToRowAtIndexPath: cell.cellPath.section == 0 ? cell.cellPath : previousIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

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