2010-12-03 22 views
21

¿Hay alguna forma de volver a cargar el encabezado/pie de página de una vista de tabla sin llamar al ?Cómo cambiar el encabezado de sección/pie de página de UITableView sin volver a cargar toda la vista de tabla

De hecho, quiero mostrar el número de celdas en la sección de una vista de tabla en el pie de página de su sección. La vista de tabla se puede editar y eliminar o insertar filas usando

– insertRowsAtIndexPaths:withRowAnimation: 
– deleteRowsAtIndexPaths:withRowAnimation: 

Parece que estos métodos no actualizan la sección de pie de página. Curiosamente, cuando llamo a estos métodos de la vista de tabla método de fuente de datos

- (NSString *)tableView:(UITableView *)table titleForFooterInSection:(NSInteger)section 

se llama (dos veces!) Pero no actualiza la vista de tabla con los nuevos valores !!

¿Alguien tiene alguna idea de cómo solucionar este problema?

Respuesta

7

Me las arreglé para hacerlo de forma indirecta: creé un UILabel y lo configuré como encabezado/pie de página de sección.

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { 
    // update sectionFooterView.text  
    return sectionFooterView; 
} 

- (void)viewDidLoad { 
    // create sectionFooterView in Interface Builder, change the frame here 
    // use Font-size:15 , BG-Color: clearColor , text-Color: RGB=(97,105,118) 
    // and Text-alignment:Center to look like table view's original footer 
    sectionFooterView.frame = CGRectMake(0, 10, 320, 12); 
} 

¿Alguien sabe una forma de hacer esto sin establecer una vista personalizada del pie de página?

+0

Esta es la manera de hacerlo. Siempre que llame a '[sectionFooterView setText: @" Some text "]' se actualizará en la pantalla. –

2

revise la documentación de UITableView, o más específicamente -reloadSections:withRowAnimation:

+0

Esto funciona pero no es el más eficiente ya que no solo actualizará el encabezado/pie de página de la sección, sino que también actualizará todas las celdas. Parece que la respuesta de Ali es el camino a seguir. –

+1

Creo que Apple espera que si cambia el pie de página o el encabezado, el contenido también cambiará. Pero sí, si desea personalizar mucho más que el texto que se muestra de forma semi-estática, entonces usar viewForFooter/Header puede ser más fácil. Solo tenga en cuenta que los encabezados y pies de página predeterminados le dan un montón de formato, y anularlos así por algo trivial tampoco puede ser la mejor opción. – Tim

32

Si sólo tiene una cabecera de texto base o pie de página, se puede acceder directamente a ellos:

[tableView footerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForFooterInSection:indexPath.section]; 

de manera similar para el encabezado:

[tableView headerViewForSection:indexPath.section].textLabel.text = [self tableView:tableView titleForHeaderInSection:indexPath.section]; 
+2

Pero acceder directamente al 'textLabel' no cambia el marco para que se ajuste al texto nuevo. ¿Tengo que llamar a 'sizeToFit' en la vista de pie de página/encabezado? – Blip

+0

Deberá llamar a beginUpdates/endUpdates para que la vista de tabla recalcule su diseño –

+0

Gold! tableView.headerViewForSection (indexPath.section) ?. textLabel.text = tableView (tableView, titleForHeaderInSection: indexPath.section) – DogCoffee

3

Aquí hay otra manera de hacerlo:

UITableViewHeaderFooterView *headerView=[self.tableView headerViewForSection:1]; 
CATransition *animation = [CATransition animation]; 
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
animation.type = kCATransitionFade; 
animation.duration = 0.35; 
[headerView.textLabel.layer addAnimation:animation forKey:@"kCATransitionFade"]; 
headerView.textLabel.text=[self tableView:self.tableView titleForHeaderInSection:1]; 
[headerView.textLabel sizeToFit]; 
11

esto debería funcionar:

UIView.setAnimationsEnabled(false) 
    tableView.beginUpdates() 

    if let containerView = tableView.footerViewForSection(0) { 
     containerView.textLabel!.text = "New footer text!" 

     containerView.sizeToFit() 
    } 

    tableView.endUpdates() 
    UIView.setAnimationsEnabled(true) 
+0

Funciona bastante bien en iOS 9, Swift 2.2.Y es mucho más limpio que los rectángulos de marcos de harcoding, lo que sin duda debería evitarse. – maganap

1

Ésta es la forma de hacerlo en Swift 3,0

tableView.beginUpdates() 
tableView.headerView(forSection: indexPath.section)?.textLabel?.text = "Some text" 
tableView.endUpdates() 
Cuestiones relacionadas