2011-05-31 21 views
9

Estoy intentando mostrar un mensaje de no resultados en mi tabla cuando está vacía. He hecho el enfoque de uilabel donde aparece cuando está vacío, pero parece que no es como Apple lo ha hecho en Contactos, etc. donde también se mueve el "Sin resultados" cuando intenta desplazarse hacia arriba y hacia abajo. El mío solo se queda allí en un solo lugar.Sin resultados - UITableView iPhone

¿Alguien sabe cómo hacer esto?

Creo que agregaron una celda Sin resultados?

Respuesta

10

Sí. Si no tiene resultados para mostrar, haga lo siguiente

  1. Crear una bandera booleana llamada noResultsToDisplay, o alguna otra cosa.
  2. Si no tiene resultados para mostrar, establezca noResultsToDisplay = YES, configúrelo en NO en caso contrario.
  3. En numberOfRowsInSection, if (noResultsToDisplay) return 3;
  4. En cellForRowAtIndexPath, if (noResultsToDisplay && indexPath.row == 2) cell.textLabel.text = @"No Results";
+0

¿pondría 2. en el viewDidLoad? Gracias. –

+0

@ K.Honda, lo siento, he editado mi respuesta. – EmptyStack

+0

Hola Simon, cuando pongo 2. en _cellForRowAtIndexPath_, obtendré 2 errores de identificador no declarado para _noResultsToDisplay_ y _numberOfRowsInSection_. ¿Sabes por qué? Gracias. –

0

Dos maneras de hacerlo: haga lo que sugiera, haga una "celda sin resultados" y tenga un estado en su tableViewController que sea BOOL resultIsEmpty = YES. En cellForRowAtIndexPath primero prueba este BOOL vacío y devuelve solo la celda sin resultado, recuerde también marcar en numberOfRintsInSection para que pueda devolver 1 en el caso de que esté vacío (de lo contrario, probablemente devolverá la longitud de la matriz del modelo que, por supuesto, 0).

La otra forma es hacer un recuadro y en la vista de tabla y colocar allí su etiqueta. Esto se puede hacer porque UITableView es una subclase de UIScrollView.

self.tableView.contentInset = UIEdgeInsetsMake(heighOfNoResultLabel, 0, 0, 0); 

Luego, en el "resultsDidLoad" o lo que su delegado para los nuevos datos se llama, se prueba si es 0 y la inserción tableView y colocar una etiqueta allí. Si no es 0, configure la inserción en

self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); 

todos los 0. Puede animar esta propiedad para que cuando no haya resultados, la vista de tabla se "desplace" hacia abajo para mostrar la etiqueta "Sin resultados".

Las soluciones de Bot son válidas, diría, aproximadamente la misma cantidad de código. La diferencia es probablemente lo que puedes hacer después.

3
#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return 3; 
} 


// Customize the appearance of table view cells. 
- (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... 

    if (indexPath.row == 2) { 
     cell.textLabel.text = @"Empty cell"; 
    } 

    return cell; 
} 
0

I han resuelto este por el mantenimiento de una vista de etiqueta con el mismo farme como la tabla tiene, y jugar con la " oculto "atributo de la etiqueta y la tabla (siempre uno es SÍ y el otro es NO).

0
i have edited the accepted answer to be look like the no search results in tableView 


    Create a boolean flag named noResultsToDisplay, or something else. 
    1-If you have no results to display then set noResultsToDisplay = YES, set it to NO otherwise. 
    2-(this step changed)In numberOfRowsInSection, if (noResultsToDisplay) return 1; 
// 1 returned not 3 

    3-(this step changed) In cellForRowAtIndexPath, 

static NSString *CellIdentifier; 
    if (noResultsToDisplay){ 
     CellIdentifier = @"Cell"; 
// in my case i use custom cell this step can be skipped if you use the default UITableViewCell 
    } 
    else{ 
     CellIdentifier = @"DocInqueryCell"; 
    } 
DocumentationInqueryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

// don't forgt to add cell in your storyboard with identifier Cell and class your custom class , again this step can be skipped if you use the default cell 

then 
if (noResultsToDisplay) { 
cell.textLabel.text = @"No Results"; 
}else{ 

do what you want in your custom cell 

} 
Cuestiones relacionadas