uitableviewcontroller tutorial example custom ios objective-c uitableview ios5 ios6

ios - tutorial - Cambie el tamaño de la altura de UITableView para que sea tan alta como sea necesario para que se ajuste solo al tamaño total del contenido



uitableview tutorial swift 4 (6)

Así que estoy teniendo un problema muy básico. Tengo este UITableView dentro de una view , y me gustaría que la altura de esta tabla sea tan alta como sea necesario para mostrar TODAS las filas dentro de la tabla. Por lo tanto, no quiero poder desplazarme, solo quiero que aparezcan todas las filas. (Las filas son dinámicas en cantidad y height ).

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CGSize cellSize = [[[_stepsArray objectAtIndex:indexPath.row]content] sizeWithFont:[UIFont boldSystemFontOfSize:18.0f] constrainedToSize:CGSizeMake(528.0f, CGFLOAT_MAX)lineBreakMode:NSLineBreakByWordWrapping]; return cellSize.height; }

Si hay 20 filas, la tabla será muy alta, pero si solo hay 1 fila, será muy pequeña y el otro contenido no aparecerá en las 19 filas siguientes.


Agregue un observador para la propiedad contentSize en la vista de tabla y ajuste el tamaño del marco en consecuencia

En viewDidLoad agregue la siguiente línea de código

[your_tableview addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];

luego en la devolución de llamada:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { CGRect frame = your_tableview.frame; frame.size = your_tableview.contentSize; your_tableview.frame = frame; }

Espero que esto te ayudará.


Aquí hay una solución más simple: todo lo que necesita es configurar sizeToFit en su UITableView , después de que se haya UITableView con celdas. Si configura delegate y dataSource través de xib , puede hacer esto en su método viewDidLoad , o viewDidAppear , como esto:

- (void)viewDidLoad { [super viewDidLoad]; [yourTableView sizeToFit]; }

Pero, si agrega delegate y dataSource en algún lugar del código, debe poner sizeToFit después de esto:

- (void)someMethod { [yourTableView setDelegate:self]; [yourTableView setDataSource:self]; [yourTableView sizeToFit]; }

Además, debes hacer esto después de recargar la tabla, si lo haces en algún lugar.

¡Esto cambiará el tamaño de su tabla exactamente a la altura que es una suma de la altura de sus celdas!


Establezca una restricción de altura en la mesa, conéctese a un tomacorriente y agregue lo siguiente a su controlador de vista:

override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() tableViewHeightContraint.constant = tableView.contentSize.height }


Puedes intentarlo así,

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row){ NSLog(@"%f",your_tableView.contentSize.height); } }

En Swift 3.0

func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.row == tableView.indexPathsForVisibleRows?.last?.row { print("/(tableView.contentSize.height)") } }


Simplemente use tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; Eso resolverá su problema.


si lo comprendo correctamente, debería poder sumar la altura de cada fila y ajustar la altura de la vista de tabla en función de eso:

CGFloat tableHeight = 0.0f; for (int i = 0; i < [_stepsArray count]; i ++) { tableHeight += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; } self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, tableHeight);

puede hacer esto inmediatamente después de [tableView reloadData]