¿Cómo eliminar celdas vacías en UITableView? [duplicar]


Esta pregunta ya tiene una respuesta aquí:

Estoy tratando de mostrar un simple UITableView con algunos datos. Deseo establecer la altura estática de UITableView para que no muestre celdas vacías al final de la tabla. ¿cómo hago eso?

Código:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSLog(@"%d", [arr count]);
    return [arr count];
}
Author: Michal, 2013-01-25

11 answers

Establezca una vista de pie de tabla de altura cero (tal vez en su método viewDidLoad), así:

Swift:

tableView.tableFooterView = UIView()

Objetivo-C:

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

Debido a que la tabla piensa que hay un pie de página para mostrar, no muestra ninguna celda más allá de las que pidió explícitamente.

Interfaz builder pro-consejo:

Si está utilizando un xib/Storyboard, simplemente puede arrastrar una UIView (con altura 0pt) en la parte inferior de la UITableView.

 809
Author: Andy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-27 23:09:17

Swift 3 sintaxis:

tableView.tableFooterView = UIView(frame: .zero)

Sintaxis de Swift:

tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

Sintaxis de Swift 2.0:

tableView.tableFooterView = UIView(frame: CGRect.zero)
 95
Author: Dustin Williams,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-11 11:57:02

En el guion gráfico, seleccione UITableView y modifique el estilo de propiedad de Plain a Grouped.

 55
Author: Gabriel Diaconescu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-18 10:17:39

Implementado con swift en Xcode 6.1

self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.tableFooterView?.hidden = true

La segunda línea de código no causa ningún efecto en la presentación, se puede utilizar para comprobar si está oculto o no.

Respuesta tomada de este enlace No se pueden ocultar celdas vacías en UITableView Swift

 14
Author: Leonardo Jorge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:41

No puedo agregar comentarios a partir de ahora, así que agregar esto como respuesta.

La respuesta de@Andy es buena y los mismos resultados se pueden lograr con la siguiente línea de código:

tableView.tableFooterView = [UIView new];

El método'new' pertenece a la clase NSObject e invoca los métodos alloc e init para UIView.

 11
Author: Sahil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-31 12:24:32

Probé el código:

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

En la sección viewDidLoad y xcode6 mostraron una advertencia. He puesto un " yo."delante de él y ahora funciona bien. así que el código de trabajo que uso es:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
 6
Author: Filozof Abi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-26 13:29:27

O puede llamar al método tableView para establecer la altura del pie de página en 1 punto, y agregará una última línea, pero también puede ocultarla, estableciendo el color de fondo del pie de página.

Código:

func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
     return 1
}

Parece la última línea

 2
Author: KostiaZzz,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-29 10:05:56

Usando UITableViewController

La solución aceptada cambiará la altura del TableViewCell. Para solucionarlo, realice los siguientes pasos:

  1. Escriba el fragmento de código que se muestra a continuación en el método ViewDidLoad.

    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

  2. Agregue el siguiente método en el archivo TableViewClass.m.

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    {
        return (cell height set on storyboard); 
    }
    

Eso es todo. Puede crear y ejecutar su proyecto.

 1
Author: Manan Devani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-08 12:59:58
override func viewWillAppear(animated: Bool) {
    self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)

/// OR 

    self.tableView.tableFooterView = UIView()
}
 1
Author: A.G,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-05 09:24:57

En el siguiente método:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (([array count]*65) > [UIScreen mainScreen].bounds.size.height - 66)
    {
        Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [array count]*65)); 
    }
    else
    {
        Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 66);
    }
    return [array count];
}

Aquí 65 es la altura de la celda y 66 es la altura de la barra de navegación en UIViewController.

 0
Author: Bharath Raj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-28 07:50:11

En viewDidLoad:

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
 -2
Author: Mahesh reddy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-08 07:03:58