uitableviewcell tableviewcontroller custom ios uitableview autolayout

ios - tableviewcontroller - Tamaño del contenido de UITableView mientras se utiliza el diseño automático



uitableview custom cell swift 4 (9)

Quiero calcular el tamaño del contenido de mi UITableView . Estoy usando autolayout con la altura UITableViewAutomaticDimension .

[UITableView contentsize] obtener [UITableView contentsize] después de recargar tableview, en ese caso, la altura se calcula en base a estimatedRowHeight .

self.tableView.estimatedRowHeight = 50; self.tableView.rowHeight = UITableViewAutomaticDimension;

Delegado -

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{     return UITableViewAutomaticDimension; }

Intentando obtener el tamaño del contenido y asignarlo a la restricción de altura.

[self.tableView reloadData]; [self.tableView layoutIfNeeded]; self.tableHeightConstraint.constant = self.tableView.contentSize.height;

¿Alguna sugerencia? Gracias por adelantado.

Editar:

Finalmente estoy solucionado mi problema con algunos tweak. Cambié la altura de la vista de tabla al máximo (en mi caso, 300 es el máximo) antes de volver a cargar los datos en la tabla, así que la vista de tabla tiene espacio para cambiar el tamaño de todas las celdas.

self.tableHeightConstraint.constant = 300; [self.tableView reloadData]; [self.tableView layoutIfNeeded]; self.tableHeightConstraint.constant = self.tableView.contentSize.height;

Gracias por la ayuda.


En mi caso, no uso la restricción de altura de tableView: con el diseño automático, la restricción superior e inferior está establecida y solo la restricción inferior cambia cuando entra / sale del modo de edición.

La respuesta de Tzegenos se modifica de esta manera: 1. agregue tableView.estimatedRowHeight = 0 en viewDidLoad () 2. agregue lo siguiente:

override func viewDidLayoutSubviews() { // 60 is row''s height as set by tableView( cellRowAt ) // places is of type [String] to be used by tableView tableView.contentSize.height = 60 * CGFloat(places.count) }


Estableciendo el valor estimado de RowHeight a cero durante la inicialización de la vista de tabla y agregando el siguiente código a viewDidLayoutSubviews funcionó para mí.

tableView.reloadData() tableView.layoutIfNeeded() tableView.constant = tableView.contentSize.height


Finalmente estoy solucionado mi problema con algunos tweak. Cambié la altura de la vista de tabla al máximo (en mi caso, 300 es el máximo) antes de volver a cargar los datos en la tabla, así que la vista de tabla tiene espacio para cambiar el tamaño de todas las celdas.

self.tableHeightConstraint.constant = 300; [self.tableView reloadData]; [self.tableView layoutIfNeeded]; self.tableHeightConstraint.constant = self.tableView.contentSize.height;

Publicar esto para que sea útil para otros.


Finalmente, entendí tu problema y aquí está la solución.

Espero que ya hayas hecho esto.

  1. En primer lugar ponemos un poco de altura fija de UITableView .
  2. Luego tome la restricción IBOutlet de UITableView Height.
  3. Luego, bajo el método viewDidLayoutSubviews , puede obtener la altura original de UITableView después de UITableView los datos.

Actualice el siguiente código:

override func viewDidLayoutSubviews() { constTableViewHeight.constant = tableView.contentSize.height }

Actualizar:

self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;

Por favor, chequee esto.


Para mí, la solución fue simplemente establecer el valor estimatedRowHeight de estimatedRowHeight en un valor mayor que cualquier valor que considerara posible para los datos que tenía que mostrar.

Por alguna razón, esto aseguró que la propiedad contentSize no se utilizara para determinar el contentSize . No muestro el indicador de desplazamiento, por lo que no me importó realmente que el estimatedRowHeight estuviera muy lejos.


Puedes probar dos cosas,

  1. Llame al método de abajo justo después de algunos segundos de retraso después de que TableView se recargue completamente.

    - (void)adjustHeightOfTableview { dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me) [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3]; }); }

  2. Cuando llama por encima del método, no devuelva estibadoHeightForRow,

    /* -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; } */ -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }

Solo inténtalo, podría ayudarte a resolver el problema.


También puede hacer una subclase de UITableView y llamar a quien quiera (es decir, su vista de contenedor) desde su layoutSubviews() donde definitivamente tiene un contentSize adecuado.


Todas las soluciones anteriores no funcionaban para mi caso. Terminé usando un observador como este:

self.tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil) override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { if let obj = object as? UITableView { if obj == self.tableView && keyPath == "contentSize" { if let newSize = change?[NSKeyValueChangeKey.newKey] as? CGSize { self.tableView.frame.size.height = newSize.height } } } }


para un uso rápido

tableView.invalidateIntrinsicContentSize() tableView.layoutIfNeeded()

después de reloadData () y

tableView.contentSize.height

Funcionará perfecto. Ejemplo en medio