2012-01-18 10 views

Respuesta

15

Puede agregar una vista de imagen en la parte superior de la vista de tabla o cambiar la vista de fondo de la vista de tabla.

// 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

Gracias, buen truco. Por cierto, ¿cómo puedo diseñar visualmente un UIView y usarlo como una capa sobre la mesa? Ya no tengo la opción de simplemente diseñar una vista con el nuevo guión gráfico XCode ... – bashan

+0

Puede diseñar la vista creando un nuevo archivo nib y cargándolo en un UIView. – simonbs

+0

Solía ​​hacerlo en el antiguo XCode. El nuevo XCode que viene con guiones gráficos parece estar funcionando de una manera diferente. No permite solo crear una nueva vista de tamaño personalizada y todas las vistas están en el mismo archivo de guión gráfico. – bashan

4

UITableView tiene una propiedad de backgroundView. Establezca esta propiedad en el UIImageView que contiene su imagen de fondo.

+0

Tenga en cuenta, que no quiero mostrar ningún fondo para la UITableView. Quiero mostrar algunos antecedentes solo cuando la tabla no tiene celdas para mostrar. Simplemente quiero ocultar la mesa y poner un fondo en su lugar. ¿Su sugerencia funcionará para eso? – bashan

0

En que numberOfRowsInSection método:

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 debe ser mayor que 0

Cuestiones relacionadas