uitableviewcell source number example data iphone objective-c cocoa-touch uitableview uiscrollview

iphone - source - Cómo desplazar UITableView a una posición específica



uitableviewcell swift 3 (7)

¿Cómo puedo desplazar la celda de la tabla a una posición específica? Tengo una tabla que muestra 3 filas (según la altura). lo que quiero es que si hago clic en la 1ª fila de acuerdo con la altura de la tabla, la 1ª fila debe desplazarse y obtener una nueva posición (centro) y lo mismo para otras filas. Intenté contenOffset pero no funcionó ..

EDITADO:

En resumen, algo así como selector de datos cuando seleccionamos cualquier fila en el selector, la fila se desplaza al centro.

Gracias..


Utilice [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:scrollPosition animated:YES]; Desplaza el receptor hasta que una fila identificada por la ruta del índice se encuentre en una ubicación particular en la pantalla.

Y

scrollToNearestSelectedRowAtScrollPosition:animated:

Desplaza la vista de tabla para que la fila seleccionada más cercana a una posición específica en la vista de tabla se encuentre en esa posición.


Vale la pena señalar que si utiliza el enfoque setContentOffset , puede hacer que su vista de tabla / colección salte un poco. Honestamente trataré de hacerlo de otra manera. Una recomendación es usar los métodos de delegación de vista de desplazamiento que se le dan de forma gratuita.


finalmente encontré ... funcionará bien cuando la tabla muestre solo 3 filas ... si las filas son más, el cambio debería ser en consecuencia ...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 30; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. cell.textLabel.text =[NSString stringWithFormat:@"Hello roe no. %d",[indexPath row]]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * theCell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath]; CGPoint tableViewCenter = [tableView contentOffset]; tableViewCenter.y += myTable.frame.size.height/2; [tableView setContentOffset:CGPointMake(0,theCell.center.y-65) animated:YES]; [tableView reloadData]; }


Simplemente una sola línea de código:

self.tblViewMessages.scrollToRow(at: IndexPath.init(row: arrayChat.count-1, section: 0), at: .bottom, animated: isAnimeted)


Versión Swift:

let indexPath:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0) self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.None, animated: true)

Enum: estas son las posiciones de desplazamiento de mesa disponibles, aquí como referencia. No necesita incluir esta sección en su código.

public enum UITableViewScrollPosition : Int { case None case Top case Middle case Bottom }

DidSelectRow:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { let theCell:UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath) if let theCell = theCell { var tableViewCenter:CGPoint = tableView.contentOffset tableViewCenter.y += tableView.frame.size.height/2 tableView.contentOffset = CGPointMake(0, theCell.center.y-65) tableView.reloadData() } }


debería funcionar usando - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated usándolo de esta manera:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [yourTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

atScrollPosition podría tomar cualquiera de estos valores:

typedef enum { UITableViewScrollPositionNone, UITableViewScrollPositionTop, UITableViewScrollPositionMiddle, UITableViewScrollPositionBottom } UITableViewScrollPosition;

Espero que esto te ayude

Aclamaciones


[tableview scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

Esto llevará su tabla vista a la primera fila.