2011-08-18 16 views

Odpowiedz

21

Tak ... To teraz działa świetnie!

stworzyłem tableView:viewForHeaderInSection: metody i stworzył UIView

UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)]; 

Potem stworzyłem UILabel & ustawić tekst ceni & kolory na etykiecie. Potem dodaje etykietę do widoku

UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)]; 
titleLabel.text = @"<Title string here>"; 
titleLabel.textColor = [UIColor whiteColor]; 
titleLabel.backgroundColor = [UIColor clearColor]; 
[customTitleView addSubview:titleLabel]; 

Więc moja metoda tableView:viewForHeaderInSection: wygląda jak ...

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

    UIView *customTitleView = [ [UIView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)]; 
    UILabel *titleLabel = [ [UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)]; 
    titleLabel.text = @"<Title string here>"; 
    titleLabel.textColor = [UIColor whiteColor]; 
    titleLabel.backgroundColor = [UIColor clearColor]; 
    [customTitleView addSubview:titleLabel]; 
    return customTitleView; 
} 

Dodajmy tableView:heightForHeaderInSection: sposobu dostarczania trochę miejsca do tytułu.

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 44; 
} 
+0

Jeśli to rozwiązało problemu, należy oznaczyć ją jako zaakceptowane. –

+0

pięknie napisane .. dziękuję, przydatne nawet teraz ;-) – thandasoru

+0

Właściwie nie musisz tego UIView. Wystarczy bezpośrednio zwrócić UILabel jako headerView jest w porządku też. – GeneCode

2

From apple documentation

Widok tabeli używa stałego styl czcionki dla tytułów sekcji nagłówka. Jeśli chcesz mieć inny styl czcionki, zamiast tego zwróć niestandardowy widok (na przykład obiekt UILabel) w metodzie delegata tableView:viewForHeaderInSection:.

Skorzystaj z poniższej metody i zwróć niestandardowy widok (UILabel) z wybraną czcionką.

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

Read from apple documentation

42

Aby użyć współrzędnych domyślne i sekcje w Tableview, czcionką białego koloru i cienia:

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 

    UILabel *label = [[UILabel alloc] init]; 
    label.frame = CGRectMake(20, 8, 320, 20); 
    label.backgroundColor = [UIColor clearColor]; 
    label.textColor = [UIColor whiteColor]; 
    label.shadowColor = [UIColor grayColor]; 
    label.shadowOffset = CGSizeMake(-1.0, 1.0); 
    label.font = [UIFont boldSystemFontOfSize:16]; 
    label.text = sectionTitle; 

    UIView *view = [[UIView alloc] init]; 
    [view addSubview:label]; 

    return view; 
} 
+1

Trochę nieszczelna pamięć –

+1

@TonyK. Czy możesz powiedzieć, gdzie? – KlimczakM

+1

Mój komentarz nie jest już istotny w ARC, ale w starszych wersjach widok musi być autoreleased. –

34

Jeśli wystarczy zmienić kolor lub czcionki w nagłówku, należy tableView: willDisplayHeaderView: forSection:. Oto przykład w SWIFT:

override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) { 

    if let view = view as? UITableViewHeaderFooterView { 
     view.backgroundView?.backgroundColor = ThemeBlue 
     view.textLabel.backgroundColor = UIColor.clearColor() 
     view.textLabel.textColor = UIColor.whiteColor() 
    } 

} 
+0

To powinna być zaakceptowana odpowiedź. –

+0

Jak ja wciąż uparcie przy użyciu Objective-C, oto jak to wygląda w tym języku: - (void) tableView: (UITableView *) tableView willDisplayHeaderView: (UIView *) Widok forSection: odcinek (NSInteger) { jeśli ([view isKindOfClass: [klasa UITableViewHeaderFooterView]]) { ... (Widok (UITableViewHeaderFooterView *)) .textLabel.textColor = [UIColor redColor]; } ... zgaduję, że powinienem spróbować tej Swiftowej rzeczy :) –

2
if ([UIDevice currentDevice].systemVersion.floatValue > 7.0) { 
    [[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]]; 
} 
Powiązane problemy