Pregunta

He cambiado el backgroundcolor/backgroundImage de mis secciones de TableViews.
Estoy usando TableView simple. No se preocupe, trabaje sin problemas con este código:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//For using an image use this:
//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

return sectionView;
}  

El problema ahora es, El título de la sección se ha ido.

Estoy configurando el título de la sección con este código:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    return @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo name];
}


}

Estoy obteniendo los títulos de la sección si no uso

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  

Necesito tu ayuda.
Muchas gracias.

Editar 1:

Gracias a Mutix:

En mi caso, la respuesta sería:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {




UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 20)] autorelease];
sectionView.backgroundColor = [UIColor greenColor];

//sectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"whiteBackground.png"]];

//Add label to view
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 20)];
titleLabel.backgroundColor = [UIColor clearColor];

//section Title
if ([tableView isEqual:self.searchDisplayController.searchResultsTableView]){
    titleLabel.text = @"";
} else {    
    id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
    titleLabel.text = [sectionInfo name];
}



[sectionView addSubview:titleLabel];
[titleLabel release];

return sectionView;

}

¿Fue útil?

Solución

los viewForHeaderInSection anula el titleForHeaderInSection método.

Para agregar un título a su encabezado cuando se usa viewForHeaderInSection Deberá agregar una etiqueta a la vista de encabezado de sección así:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *sectionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 728, 40)] autorelease];

    sectionView.backgroundColor = [UIColor greenColor];

    //Add label to view
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 728, 40)];
    titleLabel.text = @"title";
    [sectionView addSubview:titleLabel];
    [titleLabel release];

    return sectionView;
}  

Otros consejos

Tenemos una mejor solución para esto desde iOS6 en adelante:

Hay un método delegado

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section

Puedes usar este método como este

-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {

     //Set the background color of the View
     view.tintColor = [UIColor blueColor];

     // Text Color
     UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
     [header.textLabel setTextColor:[UIColor whiteColor]];

}

Implemente el método a continuación de UITableViewDelegate, y necesitaba la vista UI.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let view = UITableViewHeaderFooterView()
    view.contentView.backgroundColor = UIColor(white: 0.97, alpha: 1)
    return view
}

Si necesita cambio de pie de página, use un código similar en ... ViewforfooterInsection .. Método

Quiere dibujar su propia fuente, utilizar una personalización similar:

...    
let label = UILabel(frame: CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), tableView.sectionHeaderHeight))
label.font = UIFont(name: "HelveticaNeue", size: 14)!
label.text = self.tableView(tableView, titleForHeaderInSection: section)
view.contentView.addSubview(label)

view.textLabel.hidden = true
...

Solo agrega [titleLabel setBackgroundColor:[UIColor clearColor]];
Porque en mi caso la etiqueta superpuso el color verde.

~ Mejor solución iPhone 3.5 y 4 pantalla ~

Establezca el color posterior o la imagen de fondo de este código. Aquí no tiene que calcular para la altura de la sección, solo configurado en XIB.

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width,tableView.sectionHeaderHeight)];
    sectionView.backgroundColor = [UIColor greenColor];

    return sectionView;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top