2010-03-15 11 views
8

Szybkie pytanie, na szybką odpowiedź (ponieważ nie jestem znalezienie występują):UITableView titleForSection

Czy istnieje sposób, aby zmienić czcionkę tytule sekcji specjalistycznej (podany przez titleForSection) w iPhone?

Wielkie dzięki!

Odpowiedz

26

Będziesz musiał użyć metody viewForHeaderInSection: i podać swój własny widok. Na szczęście może to być UILabel z określoną czcionką, więc możesz to zrobić całkiem łatwo.

39

Dzięki Jasarien! Masz absolutną rację.

zostawiam mój kod tutaj, aby pomóc komuś z tym samym problemem:

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

To jest wielki, jednak UILabel jest bezpośrednią podklasą UIView, więc naprawdę nie musisz tworzyć widoku nagłówka i dodawać etykiety jako wyekspozycji. Możesz po prostu zwrócić samą etykietę jako widok nagłówka. – Jasarien

+5

Jeśli zwrócisz samą etykietę jako nagłówek, w jaki sposób nadałeś jej trochę przesunięcia po lewej? Zmiana origin.x ramki etykiety w takim przypadku nie działa. – talkol

+0

Tylko tytuł pojedynczej linii) –

5

Trzeba zastąpić

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

dać wymaganą wysokość do tytułu sekcji. Inaczej będzie nakładać się z komórką.

3

viewForHeaderInSection może działać dobrze ... ale tutaj jest alternatywa, że ​​działa dobrze dla mnie (Xcode 5 i cel 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