2010-03-15 9 views
8

Una pregunta rápida, para una respuesta rápida (ya que no estoy encontrando ninguna):UITableView titleForSection

¿Hay una manera de cambiar el tipo de letra del título de la sección (dada por titleForSection) en el iPhone?

¡Muchas gracias!

Respuesta

26

Deberá utilizar el método viewForHeaderInSection: y proporcionar su propia vista. Por suerte, esto puede ser un UILabel con una fuente especificada, por lo que puede hacerlo con bastante facilidad.

39

Gracias Jasarien! Tenías toda la razón.

He de dejar mi código aquí para ayudar a alguien con el mismo problema:

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

    NSString *sectionTitle = @"Just a title"; 

    // Create label with section title 
    UILabel *label = [[[UILabel alloc] init] autorelease]; 
    label.frame = CGRectMake(0, 0, 284, 23); 
    label.textColor = [UIColor blackColor]; 
    label.font = [UIFont fontWithName:@"Helvetica" size:14]; 
    label.text = sectionTitle; 
    label.backgroundColor = [UIColor clearColor]; 

    // Create header view and add label as a subview 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
    [view autorelease]; 
    [view addSubview:label]; 

    return view; 
} 
+10

Esto es muy bueno, sin embargo, UILabel es una subclase directa de UIView, por lo que realmente no necesita crear una vista de "encabezado" y agregar la etiqueta como una subvista. Simplemente puede devolver la etiqueta como la vista de encabezado. – Jasarien

+5

Si devuelve la etiqueta en sí misma como encabezado, ¿cómo le daría algo de desplazamiento a la izquierda? Cambiar el origen.x del marco de la etiqueta en ese caso no funciona ... – talkol

+0

Solo título de una sola línea) –

5

Tiene que reemplazar

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 

para dar altura requerida para el título de la sección. De lo contrario, se superpondrá con la celda.

3

viewForHeaderInSection podría funcionar bien ... pero aquí es una alternativa que funciona bien para mí (Xcode 5 y el objetivo iOS7):

// To set the background color and text color of the Table Headers 
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 
{ 
    // Background color 
    view.tintColor = [UIColor colorWithRed:0.329 green:0.557 blue:0.827 alpha:1.000]; 

    // Text Color & Alignment 
    UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view; 
    [header.textLabel setTextColor:[UIColor whiteColor]]; 
    [header.textLabel setTextAlignment:NSTextAlignmentCenter]; 
    // Text Font 
    UIFont *saveFont = header.textLabel.font; 
    [header.textLabel setFont:[UIFont fontWithName:saveFont.fontName size:18.0]]; 

    // Another way to set the background color 
    // Note: does not preserve gradient effect of original heade!r 
    // header.contentView.backgroundColor = [UIColor blackColor]; 
} 

Here's what it looks like