2012-01-18 16 views

risposta

15

È possibile aggiungere una visualizzazione di immagine nella parte superiore della vista tabella o modificare la vista di sfondo della vista tabella.

// Check if table view has any cells 
int sections = [self.tableView numberOfSections]; 
BOOL hasRows = NO; 
for (int i = 0; i < sections; i++) { 
    BOOL sectionHasRows = ([self.tableView numberOfRowsInSection:i] > 0) ? YES : NO; 
    if (sectionHasRows) { 
     hasRows = YES; 
     break; 
    } 
} 

if (sections == 0 || hasRows == NO) 
{ 
    UIImage *image = [UIImage imageNamed:@"test.png"]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 

    // Add image view on top of table view 
    [self.tableView addSubview:imageView]; 

    // Set the background view of the table view 
    self.tableView.backgroundView = imageView; 
} 
+0

Grazie, bel trucco. A proposito, come posso progettare visivamente un UIView e usarlo come un livello sul tavolo? Non ho più la possibilità di progettare semplicemente una vista con il nuovo storyboard XCode ... – bashan

+0

Puoi progettare la vista creando un nuovo file pennino e caricandolo in un UIView. – simonbs

+0

Ero solito farlo sul vecchio XCode. Il nuovo XCode che viene fornito con gli storyboard sembra funzionare in un modo diverso. Non ti consente solo di creare una nuova visualizzazione di dimensioni personalizzate e tutte le viste sono sullo stesso file storyboard. – bashan

4

UITableView ha una proprietà backgroundView. Impostare questa proprietà su UIImageView contenente l'immagine di sfondo.

+0

Si prega di notare, che io non voglio mostrare qualsiasi sfondo per l'UITableView. Voglio mostrare dello sfondo solo quando il tavolo non ha celle da mostrare. Voglio semplicemente nascondere il tavolo e inserire uno sfondo. Il tuo suggerimento funzionerà per questo? – bashan

0

In te numberOfRowsInSection metodo:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    if data.count > 0 { 

     return data.count 
    } 
    else{ 

     let image = UIImage(named: "Nature") 
     let noDataImage = UIImageView(image: image) 
     noDataImage.frame = CGRect(x: 0, y: 0, width: tableView.bounds.width, height: tableView.frame.height) 
     tableView.backgroundView = noDataImage 
     tableView.separatorStyle = .none 

     return 0 
    } 
} 

numberOfSections dovrebbe essere maggiore di 0