2010-07-20 12 views
8

Il mio UITableView ottiene i dati da Internet. A volte non riceve alcun dato (cella). Mostra solo un tavolo vuoto.UITableView No Data Screen

Come posso visualizzare un messaggio/una cella per l'utente che non sono stati trovati record di dati?

risposta

25

Si desidera restituire una cella che riporta la mancanza di dati.

Se si sta mantenendo i dati delle celle in una matrice che è una proprietà di classe (diciamo NSArray *listings), si può andare:

-(NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section 
{ 
    if ([self.listings count] == 0) { 
     return 1; // a single cell to report no data 
    } 
    return [self.listings count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if ([self.listings count] == 0) { 
     UITableViewCell *cell = [[[UITableViewCell alloc] init] autorelease]; 
     cell.textLabel.text = @"No records to display"; 
     //whatever else to configure your one cell you're going to return 
     return cell; 
    } 

    // go on about your business, you have listings to display 
} 
+0

è una buona idea mettere in cache questa cella come con altre cellule normali? se è così, come lo fai? – Yazzmi

+0

Non devi preoccuparti di memorizzare nella cache questa singola cella perché sta per scomparire nel momento in cui i dati effettivi vengono mostrati, e l'autorelease si prenderà cura della sua memoria per te. Non si sposterà mai fuori dal tavolo, quindi non c'è davvero niente nella cache. A proposito, vuoi farlo anche tu CARICO i tuoi dati. Quindi quando il processo di rete è terminato e hai riempito la tua fonte di dati, chiama 'reloadData' sul tuo UITableView. –

+0

funziona come un incantesimo - dovevo semplicemente aggiustare un po 'il carattere per ottenere il messaggio che volevo adattare: [cell.textLabel setFont: [UIFont fontWithName: @ Helvetica-Bold "size: 13]]; – jaySF

2

È possibile aggiungere un test in -tableView: cellForRowAtIndexPath: implementazione e visualizzazione di un UITableViewCell speciale che dice "nessun risultato disponibile" quando non si ricevono dati.

Un altro approccio, che in genere sembra migliore, è aggiungere direttamente una vista personalizzata a UITableView. Non dimenticare di rimuoverlo quando ci sono risultati da visualizzare.

2

È possibile aggiungere un UILabel alla visualizzazione Questo può essere creato a livello di codice o nel tuo xib. Se il XI ter contiene un UITableView come [vista auto], come è tipico per un UITableViewController, quindi creare una proprietà per l'etichetta sulla viewController

@property (nonatomic, retain) IBOutlet UILabel  *noResultsLabel; 

Trascinare un UILabel dalla libreria nella finestra del documento come un fratello per la tua "vista tabella". Impostare le proprietà del testo (carattere grassetto 36pt, grigio chiaro sembra piuttosto buono)
Trascinare il controllo da "Proprietario file" per collegare l'uscita all'etichetta.

UITableView with no results UILabel

Nella richiamata server, è possibile aggiungere l'etichetta alla vista. Ecco un esempio:

#pragma mark - 
#pragma mark Server callbacks 
- (void)serviceExecutionComplete:(NSMutableArray *)results { 
    [self setServerData:results]; 
    if ([results count] == 0) 
    { 
     // no results, 
     CGRect viewFrame = [[self view] frame]; 
     [[self noResultsLabel] setFrame:CGRectMake(0, (viewFrame.size.height - 100)/2, viewFrame.size.width, 50.0)]; 
     [[self view] addSubview:[self noResultsLabel]]; 
    } 
    else 
    { 
      [[self noResultsLabel] removeFromSuperview]; 
     [self.tableView reloadData]; 
    } 
} 

Si potrebbe fare la stessa cosa in tableView: numberOfRowsInSection: se che misura il vostro progetto migliore

1

stavo contemplando che mostra un emptyDataView in entrambi intestazione/piè di pagina, o hackerare uno della fila come la risposta suggerita da Dan.

Ma penso che il più pulito sia quello di creare una vista all'esterno di tableView.

@property (weak, nonatomic) IBOutlet UIView *emptyDataView; 

poi nascondere/mostrare la vista in numberOfRowsInSection: come questo:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    id sectionInfo = [[_fetchedResultsController sections] objectAtIndex:section]; 

    // Show empty data view if no data 
    _emptyDataView.hidden = ([sectionInfo numberOfObjects] == 0) ? NO : YES; 

    return [sectionInfo numberOfObjects]; 
} 

Questo funziona bene con Core Data NSFetchedResultsController e nascondere la vista una volta che ci sono dati.

1

Come accennato nel link sottostante, è facile creare un'etichetta e impostarla come vista di sfondo per UITableView come di seguito. Potrebbe essere utile per qualcuno. http://www.appcoda.com/pull-to-refresh-uitableview-empty/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    if (data) { 
     self.tableView.backgroundView = nil; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
     return 1; 

    } else { 

     // Display a message when the table is empty 
     UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; 

     messageLabel.text = @"No data is currently available. Please pull down to refresh."; 
     messageLabel.textColor = [UIColor blackColor]; 
     messageLabel.numberOfLines = 0; 
     messageLabel.textAlignment = NSTextAlignmentCenter; 
     messageLabel.font = [UIFont fontWithName:@"Palatino-Italic" size:20]; 
     [messageLabel sizeToFit]; 

     self.tableView.backgroundView = messageLabel; 
     self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 

    } 

    return 0; 
} 
+0

Ho provato questo approccio. Tieni presente che se restituisci 0 sezioni in assenza di dati, l'app si arresta in modo anomalo se si utilizza deleteRowsAtIndexPaths. Per rimediare, restituisco sempre 1 per il numero di sezioni. Ho anche aggiunto 'self.tableView.backgroundView = nil' se i dati esistono. – davew

-1

Qui uso il codice come:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
NSInteger numOfSections = 0; 
if ([jsonArray count] != 0){ 
    tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 
    numOfSections     = 1; 
    tableview.backgroundView = nil; 
} 
else{ 
    UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableview.bounds.size.width, tableview.bounds.size.height)]; 
    noDataLabel.text    = @"No item found."; 
    noDataLabel.textColor  = [UIColor blackColor]; 
    noDataLabel.textAlignment = NSTextAlignmentCenter; 
    tableview.backgroundView  = noDataLabel; 
    tableview.separatorStyle  = UITableViewCellSeparatorStyleNone; 
} 
    return numOfSections; 
} 

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section{ 
    return [jsonArray count ]; 
}